explode and substr_count not work with "\n\n" - php

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"

Related

PHP json_decode - how to use with escape quotes string?

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.

php convert single quote to double quote for line break character

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);

different output from same function with same parameters

I am confused with following string function
echo strlen("l\n2"); //give 3 in output
where as
echo strlen('l\n2'); //give 4 in output
can anybody explain why ?
Because when you use single quotes (' '), PHP does not expand the \n as a single new line character whereas in double quotes (" "), \n translates to the new line character (ie. a single character) thus giving 3 characters
Taken from PHP's String Documentation: http://php.net/manual/en/language.types.string.php
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.
\n is not parsed as a newline character when the string is wrapped in single quotes. Instead, it is treated as a literal \ followed by n.

How can characters " \n \t \r " be replaced with '-'?

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");

php single and double quotes

//remove line breaks
function safeEmail($string) {
return preg_replace( '((?:\n|\r|\t|%0A|%0D|%08|%09)+)i' , '', $string );
}
/*** example usage 1***/
$from = 'HTML Email\r\t\n';
/*** example usage 2***/
$from = "HTML Email\r\t\n";
if(strlen($from) < 100)
{
$from = safeEmail($from);
echo $from;
}
1 returns HTML Email\r\t\n while
2 returns HTML Email
what's with the quotes?
As per the PHP Documentation
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.
In other words, double quoted strings expand variables and escape sequences for special characters. Single quoted strings don't.
So in example1, with the single quoted string, the string is exactly as you see it. Slashes and all.
But in example2, rather than ending with the string \r\t\n, it ends with a carriage return, a tab and then a new line. In other words the escape sequences for special characters are expanded.
with single quotes in PHP those special characters as \n \r \t... doesn't work as expected.
According to the docs:
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.

Categories