Jump to content

Recommended Posts

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

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

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

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.