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.
Related
Hello there :) I have a script in PHP that creates a file (with the function "file_put_contents()") , and it will put the contents of a PHP file within the file it just created. Within the PHP file, there are double quotations, so as i try to implement the code that i want to put into this newly made PHP file, it has double quotations, and the way how file_put_contents works is the second part of it uses double quotations as well.
To put it into perspective, this is how it goes: file_put_contents('file.php',"code with "" in it")
so as you can see, the double quotations get in the way of the PHP files double quotes.
My question is, how do i get the text within the quotes to not parse?
Thanks
use \ more info
file_put_contents('file.php',"code with \"\" in it")
or use ' to quote second param
file_put_contents('file.php','code with "" in it')
Wrap the string which contains double quotes in a string literal defined using single quotes.
file_put_contents('file.php','code with "" in it');
"Escaping" is what you're looking for. As always, the manual holds all your answers:
https://secure.php.net/manual/en/language.types.string.php
You just need to escape the double quotes like this : file_put_contents('file.php',"code with \"\" in it")
For more information : http://php.net/manual/en/language.types.string.php
Try using addslashes("codewith dowble quotes") to over come this issue
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.
i have a little problem with eval.
here is my code:
$postbit = $template->output('posts_list_index'); // output string: $postslist it's ok
eval('echo $postbit;'); // output string: $postslist (?)
Thanks :)
I think in eval() function you can able to replace string with values. Please refer http://in3.php.net/eval.
and the solution for your's is:
eval("$postbit = ".$template->output('posts_list_index'."); ");
echo $postbit ;
try this one.. I am not sure about it.
Javadewd's Answer is correct. I prefer using double quotes instead of single quotes. PHP Manual states
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.
Eval requires that you escape your $:
eval('echo \$postbit;');
I use sprintf() on my program to output some tabs and newlines. I noticed a part of my program not working properly.
As I inspected the part that isn't working, I noticed that I used a single quote ' instead of a doublequote " and the program actually outputs a \t instead of a inivisible tab space.
I thought the two are similar and the reason for having two delimeters for php is for us to be able to insert single or doublequote in a string or echo them without inserting escape characters.
Would there be a difference in assigning variables aside from the one I discovered
$a = "qq";
$b = 'qq';
Would they be stored in the computer's memory in a different manner?
you can refer to the manual that specifies that single quotes in php consider most escape sequences as litterals, contrary ot double quotes:
http://php.net/manual/en/language.types.string.php
single quote is faster than double
double quote can parse php variable. i.e. $a=2; and if you use echo "a is: $a"; then it will print a is: 2 but single quote will print a is: $a
if you use single quotes for the format string (like you should do, since there
aren't any variable conversions to do as long as you don't need any special chars),
the given examples won't work because of the backslash before the $ (needs to be
escaped in double quoted strings - but not in single quoted!) http://php.net/manual/en/function.sprintf.php
i have problem with file() in php. The function can't read a file start with ! or $ like !textfile.txt or $textfile.txt, i try with fopen() and it happen as a same way. I have no idea how to solve this. I hope someone can help
Appreciate any help.
The filename "$textfile.txt" will not work as expected because variable interpolation happens in double quotes as a result value of variable $textfile will be appended with .txt and the result will be used as filename. If $textfile is undefined (which mostly is the case), .txt will be used as the filename.
To fix this use single quotes around the filename as '$textfile.txt' or if you have to use double quotes, escape the $ as: "\$textfile.txt"
But I see no problem with "!textfile.txt"
echo file_get_contents("\$test.txt");
Works.
You need to escape special characters or use single quotes.