homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

PHP search and replace
best way replace mutli strings?

 10:22 am on Nov 15, 2002 (gmt 0)

I'm "trying" to build a full blown CMS for the first time.

Basically I want to get the info posted in a text field and remove certain strings and replace them with my own strings and store in database.

for example I want to put <p> instead of /r/n and replace [B] with <strong> [url] with <a href= etc.

I have been using str_replace to do this - quite lengthy, so then I was looking at using arrays.

So i'm going to use

$NameOfArray=array('<', '>', '\\', '/', '=');
$text=str_replace($NameOfArray, "", $text);

I also want to reverse the process for easy viewing when someone edits the textara.

just wondering what the best function to use is or whether there is a really cool way that I have overlooked?



 1:35 pm on Nov 15, 2002 (gmt 0)

Have a look at the PHP code in this post [webmasterworld.com]

I´m not really sure whether deleting <> is such a good idea. People might like to use them as literal characters. Use htmlspecialchars to encode those characters when you display them on a page.



 3:04 pm on Nov 15, 2002 (gmt 0)

the code i'm trying to use is

$DataForm=array('<strong>', '</strong>', '<p>', '<br>', '<li>', '</li>');
$FormData=array('*b*', '*/b*', '\r\n', '\n', '[list]', '[/list]');

//to add to the DB
$text=str_replace($FormData, $DataForm, $text);
//to display in the textarea
$text=str_replace($DataForm, $FormData, $text);

which seems to work OK but Im having problems with the linebreaks it keeps adding them and also get \ in the text....is there a special way to encode these characters?


 12:55 am on Nov 16, 2002 (gmt 0)

Get, Post and Cookie values are automatically quoted if magic_quotes_gpc [php.net] is set to true. Disable automatic quoting or use stripslashes to remove the slashed.

There´s quite a bit info about linebreaks in the thread I linked to.

\r\n => <p> and \n => <br> probably does not achieve what you want. See the other thread.



 5:19 pm on Nov 17, 2002 (gmt 0)

By *no* stretch of the imagination do I call this efficient, but the 'interface' is nice:

// find $( xx )
function DoSubst ($Text, $NVPArray)

$NewStr = "";
$l = strlen ($Text);
$i = 0;

$c = 0;
while ($i < $l) {
if ($c++ > 100) break;

$x = strpos ($Text, "$(", $i);

if ($x === false) {
$NewStr .= substr ($Text, $i);
$i = $l;
} else {
$NewStr .= substr ($Text, $i, $x - $i);
$i += $x;

$Tmp = substr ($Text, $x + 2);
$y = strpos ($Tmp, ")") + $x + 2;

if ($y - $x <= 2) {
$NewStr .= substr ($Text, $x);
$i = $l;
} else {
$TagName = substr ($Text, $x + 2, $y - $x - 2);
$TagValue = urlencode ($NVPArray [$TagName]);

$NewStr .= $TagValue;

$i = $y + 1;
return ($NewStr);

SO, a sample call might look like:

$textthing = "blah$(var1)blah$(var2)blahblah";
$myarray ["var1"] = ". ";
$myarray ["var2"] = " no blah ";

echo DoSubst ($textthing, $myarray);


 6:00 pm on Nov 17, 2002 (gmt 0)

I´m not sure how your post relates to knighty´s problem. Anyway, this RE does the same as your script:

echo preg_replace('/\$\(([^)]+)\)/e', 'urlencode($myarray[\\1])', $textthing);


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved