Re: Replacing whole words only

From: Date: Wed, 22 Dec 2004 20:18:03 +0000
Subject: Re: Replacing whole words only
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-204992@lists.php.net to get a copy of this message
Chris Boget wrote:
Is there a way to replace/remove *whole* words from a phrase? For example: <? $exceptionsList = array( 'the', 'other', 'that', 'a' ); $stringToParse = 'The other lady that laughed'; if( count( $exceptionsList ) > 0 ) {
    foreach( $exceptionsList as $exceptionPhrase ) {
      $stringToParse = str_replace( $exceptionPhrase, '', trim(
$stringToParse ));
    }
    $retval = trim( $stringToParse );
} echo 'Retval: ' . $retval . '<br>'; ?> results in "The or ldy lughed" and not "lady laughed". Using the other *_replace() functions give the same results. So is what I'm looking to do even possible? thnx, Chris
$exceptions = array('the', , 'other', 'that', 'a'); $string = 'The other lady that laughed'; foreach ($exsceptions as $exception) {
      $string = str_replace(' ' . $exception . ' ', '', $string);
} You might need to convert the string to either lower- or uppercase.

« previous php.general (#204992) next »