note 86385 modified in function.str-replace by danbrown
| From: | danbrown@php.net | Date: | Thu, 16 Oct 2008 13:28:47 +0000 |
| Subject: | note 86385 modified in function.str-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-145411@lists.php.net to get a copy of this message | ||
This function is made in order to slove the problem when using conventional str_replace built-in
function when inserting tags in normal string.
<?php
$what : string to be matched wholly
$where : string to be searched in
$tag_open : opening tag string such as <font color=#123456>
$tag_close : closing tag string such as </font>
function str_html_addtag($what, $where, $tag_open, $tag_close)
{
$array_pos_begin = array();
$array_pos_end = array();
for($cnt=0; $cnt<sizeof($what); $cnt++)
{
$pos = 0;
$pos_start = $pos;
while($pos = stripos($where, $what[$cnt], $pos))//stripos - case-insensitive search
{
if(is_false($pos)) break;
array_push($array_pos_begin, $pos);
array_push($array_pos_end, $pos+strlen($what[$cnt]));
if(sizeof($array_pos_begin)>10) break;
$pos++;
}
}
array_multisort($array_pos_begin, $array_pos_end);
for($i=0; $i<sizeof($array_pos_begin)-1; $i++)
{
if($array_pos_end[$i]>=$array_pos_begin[$i+1])
{
$array_pos_end[$i] = max($array_pos_end[$i], $array_pos_end[$i+1]);
array_splice($array_pos_begin, $i+1, -(sizeof($array_pos_begin)-($i+2)));
array_splice($array_pos_end, $i+1, -(sizeof($array_pos_end)-($i+2)));
$i--;
}
}
for($i=sizeof($array_pos_begin)-1; $i>=0; $i--)
{
$head = substr($where, 0, $array_pos_end[$i]);
$tail = substr($where, $array_pos_end[$i]);
$where = $head.$tag_close.$tail;
$head = substr($where, 0, $array_pos_begin[$i]);
$tail = substr($where, $array_pos_begin[$i]);
$where = $head.$tag_open.$tail;
}
return $where;
}
?>
--was--
This function is made in order to slove the problem when using conventional str_replace built-in
function when inserting tags in normal string.
$what : string to be matched wholly
$where : string to be searched in
$tag_open : opening tag string such as <font color=#123456>
$tag_close : closing tag string such as </font>
function str_html_addtag($what, $where, $tag_open, $tag_close)
{
$array_pos_begin = array();
$array_pos_end = array();
for($cnt=0; $cnt<sizeof($what); $cnt++)
{
$pos = 0;
$pos_start = $pos;
while($pos = stripos($where, $what[$cnt], $pos))//stripos - case-insensitive search
{
if(is_false($pos)) break;
array_push($array_pos_begin, $pos);
array_push($array_pos_end, $pos+strlen($what[$cnt]));
if(sizeof($array_pos_begin)>10) break;
$pos++;
}
}
array_multisort($array_pos_begin, $array_pos_end);
for($i=0; $i<sizeof($array_pos_begin)-1; $i++)
{
if($array_pos_end[$i]>=$array_pos_begin[$i+1])
{
$array_pos_end[$i] = max($array_pos_end[$i], $array_pos_end[$i+1]);
array_splice($array_pos_begin, $i+1, -(sizeof($array_pos_begin)-($i+2)));
array_splice($array_pos_end, $i+1, -(sizeof($array_pos_end)-($i+2)));
$i--;
}
}
for($i=sizeof($array_pos_begin)-1; $i>=0; $i--)
{
$head = substr($where, 0, $array_pos_end[$i]);
$tail = substr($where, $array_pos_end[$i]);
$where = $head.$tag_close.$tail;
$head = substr($where, 0, $array_pos_begin[$i]);
$tail = substr($where, $array_pos_begin[$i]);
$where = $head.$tag_open.$tail;
}
return $where;
}
http://php.net/manual/en/function.str-replace.php