preg_replace more than one character at a time

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

preg_replace more than one character at a time

Post by simonmlewis »

Code: Select all

 $title = "$row->title"; 
 $findtitle ="/ /"; 
 $replacetitle ="-"; 
 $titlereplace = preg_replace ($findtitle, $replacetitle, $title); 
I have a problem where the title may contain spaces of course, but may also contain forward slashes.

I can go through the current DB to get rid of them, but these are idiots are will easily add more slashes. So is there a way I can do the $findtitle to look for spaces (like it does now) AND "/" in one go? Then $replacetitle replaces the spaces AND slashes with a "-"??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: preg_replace more than one character at a time

Post by Celauran »

Code: Select all

$findtitle = '/[ \/]/';
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: preg_replace more than one character at a time

Post by simonmlewis »

Crikey that was fast. Can you explain the method please?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: preg_replace more than one character at a time

Post by Celauran »

The regex matches anything enclosed in the brackets; in this case, a space and an escaped slash. You can append additional characters as needed.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: preg_replace more than one character at a time

Post by simonmlewis »

Oh I see, so if I put a ? in there, before the space or after the forward slash, it would then convert the ? into whatever I choos ein the latter part of the script?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: preg_replace more than one character at a time

Post by Celauran »

Yes, though you'd need to escape the ?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: preg_replace more than one character at a time

Post by simonmlewis »

How do I know which characters I must escape? ie. is it just special characters and not alphanum?

?:'{})(*&...etc..?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: preg_replace more than one character at a time

Post by Celauran »

Post Reply