br3nn4n Posted November 27, 2007 Share Posted November 27, 2007 I have this: <?php $article_raw=$_POST['article']; $article = str_replace ( chr(10), "<BR>", $article_raw ); ?> Which takes my textarea's text and makes the carriage-returns into <BR>'s. Works perfectly BUT--I'd like to be able to add pictures quickly by typing something like [img:jvfb112507] ...and have it automatically replace that [img:jvfb112507] with something like <div id="inline_photo"><img src="./images/articles/jvfb112507.jpg"></div> I just have no idea how to do it Any help is, as always, appreciated! Link to comment https://forums.phpfreaks.com/topic/79064-multiple-str_replaces-on-a-variable-in-one-command/ Share on other sites More sharing options...
trq Posted November 27, 2007 Share Posted November 27, 2007 str_replace also accepts arrays as both the find and replace. ps: There is a built in function nl2br that does what your first piece of code does. Link to comment https://forums.phpfreaks.com/topic/79064-multiple-str_replaces-on-a-variable-in-one-command/#findComment-400135 Share on other sites More sharing options...
br3nn4n Posted November 27, 2007 Author Share Posted November 27, 2007 Oh brilliant! should've known php has it built in haha Okay, I read the tizag.com tutorial on arrays and I think I'll try it out. Although, I still don't know how to tell it to find a string beginning with [img and use whatever value inside that to form the <div> Sorry if I'm being a pain about this Link to comment https://forums.phpfreaks.com/topic/79064-multiple-str_replaces-on-a-variable-in-one-command/#findComment-400464 Share on other sites More sharing options...
br3nn4n Posted November 30, 2007 Author Share Posted November 30, 2007 Okay, I did some outside research since I remembered about regular expressions Tell me if I'm right here--I should use preg_replace yes? Link to comment https://forums.phpfreaks.com/topic/79064-multiple-str_replaces-on-a-variable-in-one-command/#findComment-402602 Share on other sites More sharing options...
marcus Posted November 30, 2007 Share Posted November 30, 2007 $array = array('this','is','an','array','of','things','you','would','like','to','replace','with','null'); $string = "I like array's and things that you replace stuff with!"; foreach($array AS $boo){ $string = preg_replace("/$boo/is",NULL,$string); } echo $string; # should return # I 's d that stuff ! Link to comment https://forums.phpfreaks.com/topic/79064-multiple-str_replaces-on-a-variable-in-one-command/#findComment-402605 Share on other sites More sharing options...
teng84 Posted November 30, 2007 Share Posted November 30, 2007 $array = array('this','is','an','array','of','things','you','would','like','to','replace','with','null'); $string = "I like array's and things that you replace stuff with!"; foreach($array AS $boo){ $string = preg_replace("/$boo/is",NULL,$string); } echo $string; # should return # I 's d that stuff ! i dont now if that sulotion solve your prob but here is a better way i guess lol <?php $array = array('this','is','an','array','of','things','you','would','like','to','replace','with','null'); $string = "I like array's and things that you replace stuff with!"; $string = preg_replace("(".implode('|',$array)." )",NULL,$string); echo $string; # should return # I 's d that stuff ! ?> Link to comment https://forums.phpfreaks.com/topic/79064-multiple-str_replaces-on-a-variable-in-one-command/#findComment-402612 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.