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?

0 of 2 people (0%) answered Yes
Recently 0 of 2 people (0%) 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'

Dec 1st, 2003 14:08
Jakub Vrana, John Hunter, Ben Udall,


I think that there's no word-boundaries support in ereg functions, so 
you have to use preg_replace.
<?php
echo preg_replace("/\\band\\b/", "or", "sand and and");
// prints: sand or or
?>
\b stands for word-boundary in PCRE (Perl compatible regexp) functions.