[PHP]str_replace replaces all instances of a string

Results 1 to 3 of 3
  1. #1
    Member SimpleBB is offline
    MemberRank
    Aug 2009 Join Date
    58Posts

    [PHP]str_replace replaces all instances of a string

    For my bbcode parser, I'm using str_ireplace

    However, this replaces all ']' with another bbcode's markup, no matter what.

    For example,
    PHP Code:
    $message str_ireplace(array("[quot usr=\"""\"], "[/quot]"), array("<b>", "</b><i>", "</i>"), $message);
    $message = str_ireplace(array("[link href=\"""\"]""[/link]"), array("<a href=\"""\">""</a>"), $message); 
    Since the [quote] tag is parsed first, all instances of "]", even if its [link href="bob"].

    So is there anyway to solve this?
    Or do I have to use preg_replace instead?

    Lol, the code's kinda messed up cuz I'm using html tags and stuff, so just bear with it. :D

    Whoops, wrong section lol.
    Last edited by SimpleBB; 27-08-09 at 02:32 AM.


  2. #2
    Infraction Baɴɴed holthelper is offline
    MemberRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP]str_replace replaces all instances of a string

    PHP Code:
    <?php

    $mycode 
    '<?php echo("hello");?> [b]<html>[/b]';

    $stvarno = array ("<"">""[b]""[/b]");

    $zamjenjeno = array ("&lt;","&gt;""<b>""</b>");

    $code str_replace($stvarno$zamjenjeno$mycode);

    echo (
    "$code");

    ?>
    Source: http://www.trap17.com/forums/Php-Bbc...60#entry315360

    i dont know where he got it but yea... lol

  3. #3
    Member SimpleBB is offline
    MemberRank
    Aug 2009 Join Date
    58Posts

    Re: [PHP]str_replace replaces all instances of a string

    Nah, I'll just use preg_replace.

    Thanks though. :D



Advertisement