faqts : Computers : Programming : Languages : PHP : Common Problems : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

11 of 14 people (79%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

I want to replace 'and' with 'or' in a string but want it to only work on whole words. eg not 'sand'

Aug 17th, 2002 15:38
Nate Weibley,


I think you want this....
<?php
$string='One day in the sand my brother asked me "Do you want a coke 
and a sandwhich?" I said "I dont, but ask Mr. Sanders, and."';
$string=eregi_replace([^a-z0-9]+)and([^a-z0-9]
+)",'\\1or\\2',$string); //Meaning replace nonalphanumeric, then the 
sequence of letters and, then another nonalphanumeric with or... 
keeping the surrounding chars
echo $string;
?>
I hope thats what you wanted
--Nate
iamfast@tampabay.rr.com