My code:
<?php
$string="img\1\EVS\Good Habits.mp41.png";
echo str_replace('\\','/',$string);
?>
Output:
img/EVS/Good Habits.mp41.png
My original string was : img\1\EVS\Good Habits.mp41.png, but in output it removed 1.
Please tell me the reason if anyone know this ?
It's not the fault from str_replace(). If you do:
echo $string;
you will already see that you lost the number there:
img\EVS\Good Habits.mp41.png
Because your backslash escapes the 1. So the solution?
You have to escape your backslashes in your original string OR change your double quotes to single quotes, so that the escape sequence doesn't get interpreted from PHP anymore.
Related
Given the following statements:
echo 'string1'."\n";
echo 'string2';
The following gets rendered as output:
string1
string2
With these statements
echo 'string1'.'\n';
echo 'string2';
This gets rendered (note the verbatim backslash n):
string1\nstring2
When \n is in double quotes, it makes a new line as it should.
when \n is in single quotes, it will be shown in the browser as text.
Can anyone explain this behavior?
Using single quotes will mark it as a string, so PHP will literally output \n.
See here: PHP Manual
Alternatively use chr() with the ASCII code of a new line as an argument:
echo 'string'.chr(10);
Or use the <br/> Tag
echo 'string<br/>';
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters
In mysql table, i have data having this character ’.
Like:
Francesca’s Baker
But when i use json_encode it gives null instead of the string. The problem i have found is with this character ’ or similar special characters.
Any ideas how to fix this?
Have you tried this?
<?php
$data=array("test"=>utf8_encode("Francesca’s Baker"));
echo json_encode($data);
Returns {"test":"Francesca\u0092s Baker"}
Single quotes may throw errors randomly, it gets really annoying!!!
You need to escape the single quote!
There are two solutions to your problem.
Solution 1:
Replace all ' with \'
str_replace("\'","\\\'",$string);
This will replace ' with \' from the string $string.
Solution 2:
$newstring = htmlentities($string);
This will change any special characters in the string $string to html entities, so when the string $newstring is printed to the screen it will look normal.If you need further help, let me know!
I had that problem to capture json and I solved with
$data = array(
'result' => utf8_encode($xxx),
);
echo json_encode($data);
I'm trying to use str_replace to correct a filepath as shown below:
$a="F:\xampp\htdocs\yii\get_smart\Music\mix\English\1636464449";
$a=str_replace('\\','/', $a);
echo $a;
returns:
F:
mpp/htdocs/yii/get_smart/Music/mix/Englishs6464449
Can someone please tell me what I'm doing wrong?
My PHP version is 5.3.8
Use single quote for define $a
$a='F:\xampp\htdocs\yii\get_smart\Music\mix\English\1636464449';
the problem is not str_replace but the string defined within double quotes. The backslashes escape the x and other character after it.
This is happening because your string is in double quotes, so the \x is being parsed as a character.
Actually, it's trying to read \xam as a character. Docs: http://php.net/manual/en/regexp.reference.escape.php
Put your string in single quotes (or escape the slash before the x).
Your problem is that the first string has some escaped sequences. For example \xam has a meaning in php. It looks like \16 might also mean something. You should echo $a before you do the str_replace and see what you get.
I have the php function I am trying use,
echo get_products_display_price($_GET['products_id']);
my out put is "$199.99" with the quotes, is there a way to get it to output with out the " around the string without having to mess much with the function itself something like substr?
You can use trim:
echo trim(get_products_display_price($_GET['products_id']), "'\" ");
It will remove all ", 'and white spaces at the beginning and end of the string.
But I am wondering why the quotes are displayed. If you are echoing strings, normally the quotes are not printed as long as they are not part of the strings itself. You should check your get_products_display_price function and fix the error there.
echo str_replace('"','',get_products_display_price($_GET['products_id']));
or
ereg_replace('"', '', get_products_display_price($_GET['products_id']));
I have a php string with a lot of information to be displayed inside a textarea html element.
I don't have access to that textarea nor to the script (if any) that generates it.
$somestring = 'first line \nSecond line \nThird line.';
$somestring as NOT been "worked" with trim or filter_var. Nothing.
On the textfield, I get the \n printed on the textarea hence, not interpreted.
What can I try in order to have those new lines applied?
Thanks in advance.
Try wrapping $somestring with " (double quotes) instead of ' (single quotes)
\n, \r and other backslash escape characters only works in double quotes and heredoc. In single quotes and nowdoc (the single quote version of heredoc), they are read as literal \n and \r.
Example:
<?php
echo "Hello\nWorld"; // Two lines: 'Hello' and 'World'
echo 'Hello\nWorld'; // One line: literally 'Hello\nWorld'
echo <<<HEREDOC
Hello\nWorld
HEREDOC; // Same as "Hello\nWorld"
echo <<<'NOWDOC'
Hello\nWorld
NOWDOC; // Same as 'Hello\nWorld' - only works in PHP 5.3.0+
Read more about this behaviour in the PHP manual
EDIT:
The reason single and double quotes behave differently is because they are both needed in different situations.
For instance, if you would have a string with a lot of new lines, you would use double quotes:
echo "This\nstring\nhas\na\nlot\nof\nlines\n";
But if you would use a string with a lot of backslashes, such as a file name (on Windows) or a regular expression, you would use single quotes to simplify it and avoid having unexpected problems by forgetting to escape a backslash:
echo "C:\this\will\not\work"; // Prints a tab instead of \t and a newline instead of \n
echo 'C:\this\would\work'; // Prints the expected string
echo '/regular expression/'; // Best way to write a regular expression
$somestring = "first line \nSecond line \nThird line.";
http://php.net/types.string <-- extremely useful reading
this article is a cornerstone of PHP knowledge and it's just impossible to use PHP without it.
unlike most of manual pages which are are just for quick reference, this very page is one which every developer should learn by heart.