note 98498 added to function.str-ireplace

From: Date: Fri, 18 Jun 2010 19:41:03 +0000
Subject: note 98498 added to function.str-ireplace
Groups: php.notes 
Request: Send a blank email to php-notes+get-170251@lists.php.net to get a copy of this message
I've modified the search result search/replace below with this which takes the original term and replaces it with the same term with <b>tags</b> wrapped around it. Also allows multiple keywords by a space separated string, but could easily be adapted to handle an array. function bold( $subject, $keywords ) { // Replaces $findme in $subject with $replacewith // Ignores the case and do keep the original capitalization by using $1 in $replacewith // Required: PHP 5 $rest = $subject; $result = ''; foreach ( explode( " ", $keywords ) as $findme ) { while (stripos($rest, $findme) !== false) { $pos = stripos($rest, $findme); // Remove the wanted string from $rest and append it to $result $result .= substr($rest, 0, $pos); $rest = substr($rest, $pos, strlen($rest)-$pos); // Remove the wanted string from $rest and place it correctly into $result $result .= str_replace('$1', substr($rest, 0, strlen($findme)), '<b>'.'$1'.'</b>'); $rest = substr($rest, strlen($findme), strlen($rest)-strlen($findme)); } } // After the last match, append the rest $result .= $rest; return $result; } ---- Server IP: 64.71.164.2 Probable Submitter: 66.235.245.254 ---- Manual Page -- http://www.php.net/manual/en/function.str-ireplace.php Edit -- https://master.php.net/note/edit/98498 Del: integrated -- https://master.php.net/note/delete/98498/integrated Del: useless -- https://master.php.net/note/delete/98498/useless Del: bad code -- https://master.php.net/note/delete/98498/bad+code Del: spam -- https://master.php.net/note/delete/98498/spam Del: non-english -- https://master.php.net/note/delete/98498/non-english Del: in docs -- https://master.php.net/note/delete/98498/in+docs Del: other reasons-- https://master.php.net/note/delete/98498 Reject -- https://master.php.net/note/reject/98498 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170251) next »