phpnoobie9 Posted March 3, 2008 Share Posted March 3, 2008 I have a form that a user can enter in a url for an image in an input and it'll replace <!--image--> $replace=str_replace("<!--image-->",$_POST['image'],$replace); When a user enters the url I want to be able to put that inside the <img src="theirurl"> then echo out <img src="theirurl">. I don't want to enter url and have it just replace <img src="<!--image-->"> just the image comment tag because I don't want to show a blank image if the url wasn't entered. Link to comment https://forums.phpfreaks.com/topic/94128-str_replace-help/ Share on other sites More sharing options...
cooldude832 Posted March 3, 2008 Share Posted March 3, 2008 this is a bbCode function you can scrap it down to just accept bbimages [code][list] <?php function bb2html($text){ $bbcode = array("<", ">", "[ list]", "[*]", "[/list]", "[ img]", "[/img]", "[ b]", "[/b]", "[ u]", "[/u]", "[ i]", "[/i]", '[ color="', "[/color]", "[ size=\"", "[/size]", '[ url="', "[/url]", "[ mail=\"", "[/mail]", "[ quote]", "[/quote]", '"]'); $htmlcode = array("<", ">", "<ul>", "<li>", "</ul>", "<img src=\"", "\">", "<b>", "</b>", "<u>", "</u>", "<i>", "</i>", "<span style=\"color:", "</span>", "<span style=\"font-size:", "</span>", '<a href="', "</a>", "<a href=\"mailto:", "</a>", "<code>", "</code>", "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>", '">'); $newtext = str_replace($bbcode, $htmlcode, $text); //$newtext = nl2br($newtext);//second pass return $newtext; } ?> Had to put spaces in them to not get processed by the forum [/code] Link to comment https://forums.phpfreaks.com/topic/94128-str_replace-help/#findComment-482165 Share on other sites More sharing options...
phpnoobie9 Posted March 3, 2008 Author Share Posted March 3, 2008 ..? All I'm trying to do is when a user enters http://imagehostdotcom/theirimage.jpg ...for php to take that and add the image tag around it so it'll look like this: <img src="http://imagehostdotcom/theirimage.jpg "> and then take that image tag with the url and replace my image comment. Link to comment https://forums.phpfreaks.com/topic/94128-str_replace-help/#findComment-482167 Share on other sites More sharing options...
cooldude832 Posted March 3, 2008 Share Posted March 3, 2008 you should just use bbCode as its the standard for user inptuted html that way you can still run strip_tags on the input to prevent injection i.e /*$_POST['User_input'] = "<B>Hello World</b> [b]Hello World[/b] [img=http://www.phpfreaks.com/forums/index.php?action=dlattach;attach=2707;type=avatar] */ $output = strip_tags($_POST['User_input']); $output = nl2br($output); $output = bb2html($output); echo $output; /*Should be Hello World<br /><b>Hello World</b><br /><img src=\"http://www.phpfreaks.com/forums/index.php?action=dlattach;attach=2707;type=avatar\" /> */ Link to comment https://forums.phpfreaks.com/topic/94128-str_replace-help/#findComment-482172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.