Re: search/replace functions

From: Date: Thu, 04 Dec 2003 02:14:01 +0000
Subject: Re: search/replace functions
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-171756@lists.php.net to get a copy of this message
Id suggest using str_replace instead, its easy, and it will replace every ocurence of the items in the first array, with their corresponding items in the second array, the manual suggests using this instead or ereg_replace on a simple subject like this. http://www.php.net/str_replace --------old code----- for($i = 0; $i <= 31; $i++) { eregi_replace($search[$i], $replace[$i], $content); } return $content; } ---------------------- becomes -----------new code------------- $content = str_replace($search, $replace, $content); return $content; } -------------------------- Luke "Scott Ware" <scott@eardown.com> wrote in message news:1070492655.6635.10.camel@server01... > I'm new to the list, and I don't want this to sound like a stupid ?, but > I am having an issue with a function that I created. > > I have some code stored in a database, similar to XML-style tags, but I > just created them myself to be more "user-friendly" to people. > Like for instance <bold></bold>, and <red></red> (would make the text > red). > > I created a function, to search on the tags that I made, and replace > them with the appropriate HTML ones, here is my function: > > function mytags($content) > { > $search[0] = "<bold>"; > $search[1] = "<italic>"; > $search[2] = "<underline>"; > $search[3] = "<strikethrough>"; > $search[4] = "<computer>"; > $search[5] = "<red>"; > $search[6] = "<orange>"; > $search[7] = "<yellow>"; > $search[8] = "<green>"; > $search[9] = "<blue>"; > $search[10] = "<purple>"; > $search[11] = "<brown>"; > $search[12] = "<large1>"; > $search[13] = "<large2>"; > $search[14] = "<large3>"; > $search[15] = "<large4>"; > $search[16] = "</bold>"; > $search[17] = "</italic>"; > $search[18] = "</underline>"; > $search[19] = "</strikethrough>"; > $search[20] = "</computer>"; > $search[21] = "</red>"; > $search[22] = "</orange>"; > $search[23] = "</yellow>"; > $search[24] = "</green>"; > $search[25] = "</blue>"; > $search[26] = "</purple>"; > $search[27] = "</brown>"; > $search[28] = "</large1>"; > $search[29] = "</large2>"; > $search[30] = "</large3>"; > $search[31] = "</large4>"; > > $replace[0] = "<strong>"; > $replace[1] = "<em>"; > $replace[2] = "<u>"; > $replace[3] = "<s>"; > $replace[4] = "<tt>"; > $replace[5] = "<font color=\"red\">"; > $replace[6] = "<font color=\"orange\">"; > $replace[7] = "<font color=\"yellow\">"; > $replace[8] = "<font color=\"green\">"; > $replace[9] = "<font color=\"blue\">"; > $replace[10] = "<font color=\"purple\">"; > $replace[11] = "<font color=\"brown\">"; > $replace[12] = "<font size=\"+1\">"; > $replace[13] = "<font size=\"+2\">"; > $replace[14] = "<font size=\"+3\">"; > $replace[15] = "<font size=\"+4\">"; > $replace[16] = "</strong>"; > $replace[17] = "</em>"; > $replace[18] = "</u>"; > $replace[19] = "</s>"; > $replace[20] = "</tt>"; > $replace[21] = "</font>"; > $replace[22] = "</font>"; > $replace[23] = "</font>"; > $replace[24] = "</font>"; > $replace[25] = "</font>"; > $replace[26] = "</font>"; > $replace[27] = "</font>"; > $replace[28] = "</font>"; > $replace[29] = "</font>"; > $replace[30] = "</font>"; > $replace[31] = "</font>"; > > for($i = 0; $i <= 31; $i++) > { > eregi_replace($search[$i], $replace[$i], $content); > } > > return $content; > } > > my problem is, I want to be able to replace all of the "search[]" tags > with the "replace[]" ones. With the code that I have so-far I am not > able to, where the "$content" variable is the > "$row_mysql_table['content']" output. Am I on the right track? And if > not, can anyone tell me how to accomplish this? > > Thanks!

« previous php.general (#171756) next »