DJTim666 Posted August 7, 2007 Share Posted August 7, 2007 When I use the following code to replace user_input, it outputs "br" instead of "< br >". <?php $profile = $_POST[newprofile]; $user_input = array("[br]", "[u]", "[/u]", "[b]", "[/b]", "[img=", "]", "[url]", "[/url]", "[hr]", "<", "/>", ">"); $replace_with = array("<br>", "<u>", "</u>", "<b>", "</b>", "<img src='", "'>", "<a href='", "'></a>", "<hr>", "", "", ""); $new_pro = str_replace($user_input, $replace_with, $profile); ?> -- DJ Link to comment https://forums.phpfreaks.com/topic/63727-solved-str_replace/ Share on other sites More sharing options...
Crow Posted August 7, 2007 Share Posted August 7, 2007 That's because you're replacing all <'s >'s and </'s with nothing later on in the array. Basically, you're converting your [br] to < br > and then it's seeing that <br> and replacing the <>. I suggest changing your order of replacement to replace the <> first, before converting the BBesque code. Hope this helps, Steve Link to comment https://forums.phpfreaks.com/topic/63727-solved-str_replace/#findComment-317547 Share on other sites More sharing options...
wildteen88 Posted August 7, 2007 Share Posted August 7, 2007 This is what is happing: PHP is replacing [br] in to <br> correctly and returning the new value back into user_input but as PHP gets to the end of the arrays it'll remove the following symbols < and > This is why you get br instead of <br>. What you should so is replace all symbols first then convert your bbcodes into html using regex rather than str_replace. <?php if(isset($_POST['newprofile'])) { $profile = $_POST['newprofile']; // remove bad symbols using str_replace first $new_pro = str_replace(array('<', '/>', '>'), '', $profile); // now we'll convert the bbcodes using regex: $bbcodes = array( '|\[b\](.*)\[/b\]|is', '|\[i\](.*)\[/i\]|is', '|\[u\](.*)\[/u\]|is', '|\[img=([a-z]+?://){1}(.+?)\]|is', '|\[url\](.*?)\[/url\]|is', '|\[hr\]|is' ); $html = array( '<b>\\1</b>', '<i>\\1</i>', '<u>\\1</u>', '<img src="\\1\\2" />', '<a href="\\1" target="_blank" title="">\\1</a>', '<hr />' ); $new_pro = preg_replace($bbcodes, $html, $new_pro); echo nl2br($new_pro); } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <textarea name="newprofile" cols="80" rows="10"><?php echo @$_POST['newprofile']; ?></textarea><br /> <input type="submit" value="submit" /> </form> Link to comment https://forums.phpfreaks.com/topic/63727-solved-str_replace/#findComment-317602 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.