note 86385 added to function.str-replace

From: Date: Thu, 16 Oct 2008 05:49:48 +0000
Subject: note 86385 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-145400@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. $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; } ---- Server IP: 211.43.210.124 Probable Submitter: 119.197.3.10 ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit -- https://master.php.net/note/edit/86385 Del: integrated -- https://master.php.net/note/delete/86385/integrated Del: useless -- https://master.php.net/note/delete/86385/useless Del: bad code -- https://master.php.net/note/delete/86385/bad+code Del: spam -- https://master.php.net/note/delete/86385/spam Del: non-english -- https://master.php.net/note/delete/86385/non-english Del: in docs -- https://master.php.net/note/delete/86385/in+docs Del: other reasons-- https://master.php.net/note/delete/86385 Reject -- https://master.php.net/note/reject/86385 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145400) next »