str_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

str_replace

Post 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
^
&
[
]
(
)
{
}
+
!
?
=
´
#
$
tech603
Forum Commoner
Posts: 84
Joined: Thu Mar 19, 2009 12:27 am

Re: str_replace

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: str_replace

Post 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.
Post Reply