I need to replace < and > with &lt; and &gt; in an HTML string. The problem is that I don't want to replace ALL occurrences of < and >, just the ones inside bbcode-like tags:

[javascript] ... these < > should get replaced with entities ... [/javascript] 
these shouldn't because they are outside bbcode tags.

So far I've got:

$text = str_replace(array('<', '>'), array('&lt;', '&gt;'), $text);

but this replaces everything...

Any ideas?

    doing this might be very complex. one suggestion is to do this:

    preg_match_all('#\[(javascript|other_bbctag)\](.|\s)*?\[/(javascript|other_bbctag)\]#i',$text,$matches);
    for($i=0;$i<=count($matches[0]);$i++){
       $rand[$i]=md5(rand(1,100000).rand(1,100000));
       $text=str_replace($matches[0][$i],$rand[$i],$text); //encrypt text in bbc tags
    }
    //now replace
    $text = str_replace(array('<', '>'), array('&lt;', '&gt;'), $text); 
    //now reconstitute
    for($i=0;$i<=count($matches[0]);$i++){
       $text=str_replace($rand[$i],$matches[0][$i],$text);
    }
    

    I'm fairly sure my regular expression pattern will work, but you'd have to enumerate the bbctags you wish to protect or I [could] use square brakets elsewhere [/could] I not?

    Samuel

      function replaceBrackets($arr)
      {
         return $arr[1] . htmlspecialchars($arr[3]) . $arr[4];
      }
      $regexp = '|(\[([^\]]*)\])(.*)(\[/\2\])|Usi';
      $output = preg_replace_callback($regexp, 'replaceBrackets', $text);
      

      If you want it only to apply to a specific tag:

      function replaceBrackets($arr)
      {
         return $arr[1] . htmlspecialchars($arr[2]) . $arr[3];
      }
      $regexp = '|(\[javascript\])(.*)(\[/javascript\])|Usi';
      $output = preg_replace_callback($regexp, 'replaceBrackets', $text);
      

        Perhaps something like this?

        $str = '<sometag>[javascript] ... these [i]< >[/i] should get [b]replaced[/b] with entities ... [/javascript]</sometag>
        these shouldn\'t because they are outside bbcode tags.';
        
        $pattern = '#\[([^]]+)\].*?\[/\\1\]#s';
        
        function replace_angleBrackets($a){
        	return str_replace(array('<', '>'), array('&lt;', '&gt;'), $a[0]);
        }
        
        $test = preg_replace_callback($pattern, 'replace_angleBrackets', $str);
        echo $test;
        

        Output (via richt-click view source):

        <sometag>[javascript] ... these [i]&lt; &gt;[/i] should get [b]replaced[/b] with entities ... [/javascript]</sometag> 
        these shouldn't because they are outside bbcode tags.
        

          Never mind my previous idea - it was flawed. 🙂

            Thank you guys! This was exactly what I was looking for.

              I just realized that there is a typo in the thread's title "Replcae" instead of "Replace". Maybe one of the admins can fix it so that other people can find it easier.

                poisa;10904751 wrote:

                I just realized that there is a typo in the thread's title "Replcae" instead of "Replace". Maybe one of the admins can fix it so that other people can find it easier.

                Oh, we can do that with str_replace right here! 😉

                $str = '[QUOTE=poisa;10904751]I just realized that there is a typo in the thread\'s title "Replcae" instead of "Replace". Maybe one of the admins can fix it so that other people can find it easier.[/QUOTE]';
                $str = str_replace('Replcae', 'Replace', $str);
                echo $str;
                

                Sorry.. just some dry humor thrown into the mix.

                  Write a Reply...