I have a string coming from a language file containing strings with the text in the current language.
$str = 'blabla\n\nmore blabla';
$str is going to be used in an textarea where the \n must be a linebreak
If I place it inside double quotes this works.
The problem is that $str will always be in single quotes. I've been Googling and searching this site. There are many similar questions, but I didn't manage to find a solution.
How can I convert my single-quoted string (with a literal "\n") to a doublequoted string (where "\n" is converted to a linebreak)?
$str = str_replace('\n', "\n", $str);
Related
I have this string:
{\"sub\":\"value\"}
How I can use json_decode on this string?
When I try do it in that way:
$text = '{\"sub\":\"value\"}';
$json = json_decode($text, true);
var_dump($json);
I got NULL as result.
I know, I can use something like that:
$text = str_replace('\"', '"', $text);
But it also return null, because my original string is more extensive.
Real json_string you can found here: https://www.olx.pl/oferta/praca/praca-w-sklepie-internetowym-CID4-IDP2Wy2.html
It start from: window.__PRERENDERED_STATE__= " and end at the end of this code line.
Thanks for your help.
According to PHP: Strings - Manual escape sequences will not be expanded when they occur in single quoted strings.
Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings
That means your string $text = '{\"sub\":\"value\"}'; will be treated as string with separate \ and " characters, which is invalid JSON string.
You need to use double quotes around string $text = "{\"sub\":\"value\"}"; in that case \" will be expanded, therefore you will get a valid JSON string.
Hi guys why i can't explode or count character with "\n\n" in my string?
$input = 'sv_privateClientsForClients\\0\\sv_pure\\0\n0 0 0 0 999 \"DarkGhost\"\"spectator\"\n\n0 0 0 0 999 \"MaximuM\"\"spectator\"\n\n",';
$str = substr($input, strpos($input, "sv_pure") + 11, -7);
$x = explode('\n\n', $str); //not work
$c = substr_count($str,"\n\n"); // not work
$input = 'sv_privateClientsForClients\\0\\sv_pure\\0\n0 0 0 0 999 \"DarkGhost\"\"spectator\"\n\n0 0 0 0 999 \"MaximuM\"\"spectator\"\n\n",';
$str = substr($input, strpos($input, "sv_pure") + 11, -7);
$x = explode('\n\n', $str);
$c = substr_count($str,'\n\n'); //changed double quotes to single quotes
Try to replace '\n' with a "ABC" using str_replace() function and then explode string with "ABC"
Do you want a pair of newline characters, or do you literally want the string \n\n in the output? The answer hinges on the answer to that question, but in either case by far the most important thing you can do is be consistent with the style of quotes you use.
PHP strings can be in single quotes (''), or in double quotes ("") as well as a couple of other formats that we won't get into for the sake of simplicity. Single quotes and double quotes are not the same:
Double quote strings support the inclusion of a number of control characters (\n for newlines, \t for tabs, etc), while single quote strings support almost no control characters. If you want a newline in a single quote string you have to literally put a newline into the string.
Double quote strings support variable substitution (if you put a named variable in the string then the contents of the named variable will be substituted when you echo the string out).
The fact that your code is using single-quoted strings for some things and double-quoted strings for others means that your strings are inconsistent. "\n\n" will not match '\n\n' because they are not the same thing.
If you intend for \n\n to mean a pair of newlines then you should simply just use double-quoted strings throughout.
If you intend for \n\n to mean the literal string '\n\n' then you can either use single-quoted strings throughout, or you could use the escape sequence \\ which tells PHP that the next character is not a control character but a literal backslash. To get \n\n with a double-quoted string you need to enter it into your code as "\\n\\n"
i get from db a text like this.
{br}{/br}hello!{br}{/br}
this text is outputted inside a textarea element.
what i need is to replace all the '{br}{/br}' with invisible char '\n' which should set a enter space in the textarea itself. hoping :)
what i tryed to do is.
$text = str_replace('{br}','\n',$text);
$text = str_replace('{/br}','\n',$text);
then output $text in textarea, but chars \n are visible :|
Use str_replace with a double quoted "\n" for it to be interpreted as a newline; '\n' with single quotes is a literal backslash followed by an n.
$text = str_replace('{br}{/br}', "\n", $text);
I'm not sure why you're calling str_replace once for {br} and once for {/br}. Do you want each pair of {br}{/br} to be replaced by two new lines? If so, you could do that more simply with a single call:
$text = str_replace('{br}{/br}', "\n\n", $text);
You need to put the \n in double quotes, not single quotes. Variables and escape sequences are not interpolated in single quotes. Also, you probably want to replace the whole string {br}{/br} with a single new line - with what you have done you will replace it with two.
So:
$text = str_replace('{br}{/br}',"\n",$text);
Is probably what you want. It's probably worth you reading this so you know what you can/can't do with strings in PHP.
Try using double quotes
$text = str_replace('{/br}', "\n", $text);
How can characters " \n \t \r " be replaced with '-' ?
echo preg_replace('/\s/','-','\n\t\n\r\n');//output '\n\t\n\r\n' instead should be'-----'
Edit: I have dynamic content in real app like:
preg_replace('/\s/','-',$_Request['content']);
can I fix it by adding "" around variable?
preg_replace('/\s/','-',"$_Request['content']");
Edit2:
How can be string converted from format 'str' to format "str"?
Thanks
Well, two things. First, the problem is single quotes in your replacement string. Meta-Characters (\n\t\r, etc) are not processed inside of single quotes.
However, don't use a regex for this. There's no need for the complexity of the regex. Use
Either use str_replace:
echo str_replace(array("\r", "\n", "\t", "\v"), '-', "\r\n\t\r\v\n\t");
Or strtr:
echo strtr("\r\n\t\r\v\n\t", "\r\n\t\v", '----');
Edit: Ahh, now I see what you're getting at. You have a string with a literal \r\n\t\r\v\n\t in it, and want to replace them out. Well, you can do that via regex:
$regex = '/(\s|\\\\[rntv]{1})/';
$string = preg_replace($regex, '-', $_GET['content']);
Basically, it matches any space character, and any literal \ followed by either r, n, t or v...
If you are looking to replace the actual whitespace characters, you need to enclose the input string in double quotes (") so PHP converts the escape sequences for you:
echo preg_replace('/\s/', '-', "\n\t\n\r\n");
Else if the escape sequences occur literally (i.e. you see \n\t\n\r\n instead of line feed, tab, line feed, carriage return, line feed), you need to replace by the following character class (and keep single quotes (') on the input string):
echo preg_replace('/\\\\[rnt]/', '-', '\n\t\n\r\n');
You ought to be passing content through $_POST instead of $_GET, I don't know how PHP handles tabs, newlines and returns in GET variables.
You are using 's instead of "s. You should change your code to:
echo preg_replace('/\s/','-',"\n\t\n\r\n");
See here: single-quoted and double-quoted.
http://www.php.net/manual/en/language.types.string.php
There's also a string method for that:
echo strtr($str, "\r\n\t\v ", "-----");
If you want to remove linebreaks but retain spaces, then remove the trailing and the fifth -.
Since you seemingly want literal \r and \n converted, you need to use a map (or even a regex) like:
echo strtr($str, array('\\r'=>"\r", '\\n'=>"\n", '\t'=>"\t", ' '=>"␣"));
// single quoted strings escaped twice for illustration
Try:
echo preg_replace('/\s/','-',"\n\t\n\r\n");
Note the double quotes on the string.
If you enclose a string with single quotes, special characters lose their special meaning:
echo preg_replace('/\s/','-',"\n\t\n\r\n");
I've to replace newline (\n) with & in a string so that the received data could be parsed with parse_str() into array. The thing is that when I put \n in single quote it somehow turns out as to be replaced with a space:
str_ireplace(array('&', '+', '\n'), array('', '', '&'), $response)
"id=1 name=name gender=gender age=age friends=friends"
But when I put \n in double quotes then it works just fine:
str_ireplace(array('&', '+', "\n"), array('', '', '&'), $response)
"id=1&name=name&gender=gender&age=age&friends=friends"
Why is that so?
Because only the escaped sequences \' and \\ have a meaning in single quoted strings.
See the documentation:
To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.
Update:
Another difference is that PHP only substitutes variables inside double-quoted strings (and heredoc). Therefore you can consider processing of single-quoted strings to be faster in general (but maybe not measurably faster).
Btw you don't necessarily need to use str_ireplace as &, + and \n have no upper or lower case version. There is just one version, so str_replace would be enough.