We take a look at Ubisoft's next iteration in the Far Cry universe to see just what is on offer
Badger Badger Badger - Far Cry 4 Reviewed
We chat with Blizzard RTS veteran Allen Dilling on the past, present and future of StarCraft!
StarCraft II: Legacy of the Void Developer Interview
The first expansion for Company of Heroes 2 has landed. How does one attack Adrennes Assault? Read our review to find out!
Company of Heroes 2: Adrennes Assault Review
PHP strings question
thermite
Brisbane, Queensland
9644 posts
Hey guys this is doing my head in.
I have a textfield where a user can configure a custom line ending like '\r\n' or '\n', etc...

Now the problem is that when I output that into a text file it literally says \r\n instead of being a line break.

If I'd hardcoded "\r\n" with double quotes, it would work. But if I'd done it with single quotes I would get this same behaviour.

So basically I need some way to convert a single quote string to a double quote string.

I tried some stuff with preg_replace() but nothing is working.

Also don't want to manually replace '\n' with "\n" for instance, I want something more flexible.
02:30pm 16/05/12 Permalink
system
Internet
--
02:30pm 16/05/12 Permalink
Hogfather
Cairns, Queensland
12409 posts
\n isn't literally \n.

If you aren't using a multi-line input then you'll need to manually find and replace \n.
02:32pm 16/05/12 Permalink
trog
AGN Admin
Brisbane, Queensland
36161 posts
So basically I need some way to convert a single quote string to a double quote string.
I don't really understand what you're trying to do... is the stuff you want to convert in a database, or its only getting converted when a form is submitted?
02:51pm 16/05/12 Permalink
thermite
Brisbane, Queensland
9645 posts
Neither trog, it's upon using the user submitted value in text, I want to put it in as what they intended the characters to mean, not as the text that they entered.

I'll just use this


$replace = array(
'\n' => "\n",
'\r' => "\r",
'\t' => "\t",
'\v' => "\v",
'\e' => "\e",
'\f' => "\f",
);
return str_replace(array_keys($replace), array_values($replace), $string);


That covers all the weird untypable things.
02:57pm 16/05/12 Permalink
Jim
UK
12897 posts
I started typing a similar post to trog's asking whether you want the user to be able include a single/double quote literally as part of their end of line sequence for some reason, otherwise what's the problem with just using str_replace to remove single/double quotes and using what's left directly?

eg if you do something like:

$quotes = array("'", "\"");
str_replace($quotes, "", $endline);

then fputs($fp, $line.$endline); you shouldn't get any literal \r\n in your text file
03:05pm 16/05/12 Permalink
thermite
Brisbane, Queensland
9646 posts
The user isn't typing quotes in. I just wanted what they typed in, in a single line textfield, to behave as though it was hardcoded into the code with double quotes around it.

I guess I could add double quotes around it and eval it... !
But my str_replace thing seems to be working.

last edited by thermite at 15:15:35 16/May/12
03:13pm 16/05/12 Permalink
Jim
UK
12898 posts
I guess I don't properly understand what you're trying to do

I have a textfield where a user can configure a custom line ending like '\r\n' or '\n', etc...
I took this to mean they are entering some data, and then there's another field where they enter the type of line end they want.
if so, why not simply append that to the data/text they supplied?

I think I'm being thrown off by all the business with the single and double quotes, if they're not being entered by the user how are they even coming into it?

sounds like you have a solution anyway
03:24pm 16/05/12 Permalink
thermite
Brisbane, Queensland
9647 posts
Yeah the only reason I mentioned single and double quotes is because I can't find the words to explain what I'm saying. So I couldn't google this problem either.
The user isn't supplying any other text. That comes from elsewhere.
03:40pm 16/05/12 Permalink
Raven
Melbourne, Victoria
6880 posts
When a user enters \n, they're entering '\\' . 'n', or "\\n". It's a little tricky to portray.
Essentially, you want to preg_replace "\\n" with '\n'.

Also,
define('CR', '\r');
define('LF', '\n');
define('CRLF', CR . LF);

It'll make your life easier in the long run.
04:04pm 16/05/12 Permalink
gamer
2504 posts
This post has been removed.
Reason: Ignorant dribble, again
Send Private Message
04:17pm 16/05/12 Permalink
scuzzy
Brisbane, Queensland
15347 posts
I totally can't picture what you're trying to do here in code
04:23pm 16/05/12 Permalink
Whoop
Brisbane, Queensland
19894 posts
Looks like he has a single line input box, someone can come along and enter text \n like this \n and it'll automatically replace \n the backslash n with a newline character.

Why the hell not have a multi line input box? I can't imagine entering a whole paragraph of text on a single line with a bunch of \n's will be ideal.
05:20pm 16/05/12 Permalink
thermite
Brisbane, Queensland
9648 posts
They won't be entering a paragraph, they are configuring custom delimiters for a file format. This way the next time they visit the config they won't just see an empty box with a line break in it, they'll be able to see exactly which characters (unix EOL vs windows EOL...) or any other 'invisible' characters are entered.
05:35pm 16/05/12 Permalink
Dazhel
Gold Coast, Queensland
4841 posts
Does it have to be stupidly flexible with weird and wonderful line ending markers?
I'd be tempted to constrain it if possible and change the text input to a drop down that has three options: CR, LF, and CRLF
06:15pm 16/05/12 Permalink
scuzzy
Brisbane, Queensland
15349 posts
Tabs bro, tabs
07:47pm 16/05/12 Permalink
kos
Germany
2042 posts
Yeah the only reason I mentioned single and double quotes is because I can't find the words to explain what I'm saying. So I couldn't google this problem either.
The user isn't supplying any other text. That comes from elsewhere.

I think the commonly used term for them is still just single quoted and double quoted string literals, but I think double quoted string literals are also referred to as something like variable-parsed string literals. Unfortunately I'm pretty sure there's no simple way to parse a string variable in the same way that a double quoted string literal is parsed by PHP.

I think the closest you're going to get to exactly what you were looking for is by using eval, which is very undesirable because it can literally run any maliciously injected code if the user knows how the variable is eval'd by your code. But this line of code will do what you're looking for:
eval("\$str = \"$str\";");

In the end, though, I think your solution of just replacing characters in the string is probably simpler and safer.
05:10am 17/05/12 Permalink
gamer
2505 posts
This post has been removed.
Reason: Abusive
Send Private Message
09:22am 17/05/12 Permalink
AusGamers Forums
Show: per page
1
This thread is archived and cannot be replied to.