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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.