Re: Replacing whole words only

From: Date: Wed, 22 Dec 2004 16:00:54 +0000
Subject: Re: Replacing whole words only
Groups: php.general 
Request: Send a blank email to php-general+get-204967@lists.php.net to get a copy of this message
I'm also an idiot ... Something else, the "or" output is occurring because the exception "the" matches "other " first so "other" becomes "or" minus the "the"... So heres what I did ... $search = array( '/\bthe\b/', '/\bother\b/', '/\bthat\b/', '/\ba\b/' ); $string = 'The other lady that laughed'; if( count( $search ) > 0 ) { foreach( $search as $exceptionPhrase ) { $string = preg_replace( strtolower($exceptionPhrase), '', strtolower($string)); } $retval = trim( $string ); } echo 'Retval: ' . $retval . '<br>'; And got lady laughed In the matches ex. "/\bthe\b/" the \b mean word boundry so it matches only the whole word... -David

« previous php.general (#204967) next »