note 36810 modified in function.str-replace by aidan
| From: | aidan@php.net | Date: | Fri, 25 Jun 2004 06:05:48 +0000 |
| Subject: | note 36810 modified in function.str-replace by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71638@lists.php.net to get a copy of this message | ||
I got sick of trying to replace just a word, so I decided I would write my own string replacement
code. When that code because far to big and a little faulty I decided to use a simple preg_replace:
<?php
function word_replace($search, $replace, $subject) {
/*-------------------------------------------------------
Written by Rowan Lewis of PixelCarnage.com
---------------------------------------------------------
$search(string), the string to be searched for
$replace(string), the string to replace $search
$subject(string), the string to be searched in
-------------------------------------------------------*/
return preg_replace('/[a-zA-Z]+/e', '\'\0\' == \'' . $search .
'\' ? \'' . $replace . '\': \'\0\';', $subject);
}
?>
I hope that this code helpes someone!
--was--
I got sick of trying to replace just a word, so I decided I would write my own string replacement
code. When that code because far to big and a little faulty I decided to use a simple preg_replace:
function word_replace($search, $replace, $subject) {
/*-------------------------------------------------------
Written by Rowan Lewis of PixelCarnage.com
---------------------------------------------------------
$search(string), the string to be searched for
$replace(string), the string to replace $search
$subject(string), the string to be searched in
-------------------------------------------------------*/
return preg_replace('/[a-zA-Z]+/e', '\'\0\' == \'' . $search .
'\' ? \'' . $replace . '\': \'\0\';', $subject);
}
I hope that this code helpes someone!
http://php.net/manual/en/function.str-replace.php