note 108134 added to function.str-replace

From: Date: Sat, 31 Mar 2012 05:26:09 +0000
Subject: note 108134 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-186924@lists.php.net to get a copy of this message
I found a pretty low tech solution to avoid the "gotcha" without worrying about the array order of how things are replaced. I could not "order" the replacement array easily because it was being read from a database table. Anyway if you add an identifiable token to each replaced word, then just filter this out at the very end, no nested search terms are found. I just dynamically add the %% after the first char of each word before pumping it into the str_ireplace function. $find = array("as1", "as2", "as3", "flex"); $replace = array("<a href = \"#as1\">A%%uto S%%entry R%%ev. A%%</a>", "<a href = \"#as2\">A%%uto S%%entry E%%xp</a>", "<a href = \"#as3\">A%%uto S%%entry f%%lex</a>", "<a style = \"color: red;\" href = \"#flex\">f%%lex</a>"); $text = str_ireplace($find, $replace, $text); echo str_ireplace("%%", "", $text); In this case I am using %% as my token as this is an unlikely char combo for me. ---- Server IP: 192.41.42.26 Probable Submitter: 24.229.154.146 ---- Manual Page -- http://www.php.net/manual/en/function.str-replace.php Edit -- https://master.php.net/note/edit/108134 Del: integrated -- https://master.php.net/note/delete/108134/integrated Del: useless -- https://master.php.net/note/delete/108134/useless Del: bad code -- https://master.php.net/note/delete/108134/bad+code Del: spam -- https://master.php.net/note/delete/108134/spam Del: non-english -- https://master.php.net/note/delete/108134/non-english Del: in docs -- https://master.php.net/note/delete/108134/in+docs Del: other reasons-- https://master.php.net/note/delete/108134 Reject -- https://master.php.net/note/reject/108134 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186924) next »