Page 1 of 1
str_replace
Posted: Wed May 13, 2009 6:28 pm
by JKM
Code: Select all
$newfile = str_replace(" ", "\ ", $text);
$newfile2 = str_replace("^", "\^", $newfile1);
$newfile3 = str_replace("&", "\&", $newfile2);
This didn't work out so good (returned nothing), so how should I solve this issue?
Edit: I want to replace the following caracters (I could post all of them, due to some mysql error):
empty space
^
&
[
]
(
)
{
}
+
!
?
=
ยด
#
$
Re: str_replace
Posted: Wed May 13, 2009 7:03 pm
by tech603
If your going to be replacing multiple characters you may wish to use preg_replace instead of doing multiple string replaces. This way you have it build a pattern of characters you want to replace.
http://us.php.net/manual/en/function.preg-replace.php
Hope that helps.
Re: str_replace
Posted: Wed May 13, 2009 9:32 pm
by requinix
Au contraire, if you want to replace multiple strings
without needing the power of regular expressions, use the array-syntax version of
str_replace:
Code: Select all
string str_replace(array $search, array $replace, string $subject)
But if all you want to do is add slashes ("escape") some characters then use
addcslashes.