Character replacement

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
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Character replacement

Post by visionmaster »

I have a string which eventually has characters I don't want and all of these unallowed characters should be replaced by a pipe |

For example:
", "
" ,"
"| "
" |"
"\""
"\\"

These characters should be replaced by a pipe.

What ist the quickest and most effective way to do that? String fuction or RegExps?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Probably something like:

Code: Select all

$replace = array(', ', ' ,', '| ', ' |', '\''); //array of stuff to replace
$string = str_replace($replace, '|', $string);
Post Reply