Jump to content

str_replace help...


phpnoobie9

Recommended Posts

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

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

..? 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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.