[PHP] help using brackets as text

Discussion in 'Web Design & Coding' started by CHiLLaXen, Aug 6, 2004.

  1. CHiLLaXen

    CHiLLaXen hypnotika

    Messages:
    107
    Location:
    Minnesota
    ok, here is an example:

    Code:
    $search = "/[BOLD]/"
    $replace = "<b>"
    
    preg_replace($search, $replace, $str);
    
    I get

    Warning: Unknown modifier 'B'

    how can I get it so it reads a bracket as text and not as a modifier thingy :confused:
     
  2. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    Code:
    $search = "/[BOLD]/"; 
    $replace = "<b>";
    preg_replace($search, $replace, $str);
    I think your problem lies in the fact that you don't have ; after each finished line of code. You can also try adding a \ before each bracket, so you would get "/\[BOLD\]/".

    (Thanks btw, you solved a problem I had for a long time with PHP :D Though very simple, I just couldn't get myself to use preg_replace :doh:
     
  3. CHiLLaXen

    CHiLLaXen hypnotika

    Messages:
    107
    Location:
    Minnesota
    oops yea, I had ; there, just not when I retyped it :p , but I have another question.

    if I have "/[/CODE]/"

    how can I make that work, like [ CODE ] begins code and [ /CODE ] ends code?

    Keep getting a Warning: not a modifier '/'
     
  4. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    Same thing, add a \ before the /, like "/\[\/CODE\]/". Damn, that are a lot of slashes :p A \ means PHP should output/use the character, even when it's a special character, like [, /, etc.
     
  5. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    the \ is the escape character which means that php or the preg should treat that character as a literal with no special attributes. The \ is special so if you want a literal one you will need to use \\

    I think preg_quote() will do this to a certain extent, you might want to write a little test file to see how it changes strings.
     
  6. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    I am sure gonna test this at home :D
     
  7. CHiLLaXen

    CHiLLaXen hypnotika

    Messages:
    107
    Location:
    Minnesota
    ok, I got most of it work excecpt there is a little bug in my code
    PHP:
    function bracket2html($str)
    {
       
    $search[0]  = "/\[CODE\](.+)\[\/CODE\]/";           $replace[0]  = "<br /><div style=\"width: 95%; padding: 0px 5px 10px 5px; background-color: #999999; border: 1px solid #404040;\"><font style=\"font-family: Courier new;\">\\1</font></div><br />";
       
    //some more $search and $replace arrays

       
    $str preg_replace($search$replace$str);
       
    $str str_replace("\n""<br />"$str);
       return 
    $str;
    }
    it will find the [ CODE ] correctly but when trying to get the [ /CODE ], it wil go all the way to the last [ /CODE ] in the $str. How can I make it stop when it finds the first [ /CODE ]? :confused:

    *edit* here is the rest of the search/replace array:
    PHP:
       $search[1]  = "/\[SNIP\](.+)\[\/SNIP\]/";
          
    $replace[1]  = "<font style=\"font-family: courier new;\">\\1</font>";
       
    $search[2]  = "/\[B\](.+)\[\/B\]/";
          
    $replace[2]  = "<b>\\1</b>";
       
    $search[3]  = "/\[BOLD\](.+)\[\/BOLD\]/";
          
    $replace[3]  = "<b>\\1</b>";
       
    $search[4]  = "/\[U\](.+)\[\/U\]/";
          
    $replace[4]  = "<u>\\1</u>";
       
    $search[5]  = "/\[UNDERLINE\](.+)\[\/UNDERLINE\]/";
          
    $replace[5]  = "<u>\\1</u>";
       
    $search[6]  = "/\[LINK=\"(.+)\"\](.+)\[\/LINK\]/";
          
    $replace[6]  = "<a href=\"\\1\">\\2</a>";
       
    $search[7]  = "/\[IMG=\"(.+)\"\]/";
          
    $replace[7]  = "<img src=\"images/\\1\" />";
    they all dont work :mad:
     
  8. CHiLLaXen

    CHiLLaXen hypnotika

    Messages:
    107
    Location:
    Minnesota
    YES! I found out why it wasn't working. instead of using (.+) I have to use (.*?)
     
  9. a1ehouse

    a1ehouse Chamone M*tha Fu*ka

    Messages:
    288
    Ah the beauty of regular expressions!! Check your expressions using the Reg Ex Coach: http://weitz.de/regex-coach/
     
  10. CHiLLaXen

    CHiLLaXen hypnotika

    Messages:
    107
    Location:
    Minnesota
    Just what I needed! I just learned about Regex / modifier stuff today :p
     
  11. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    Now I need your help:
    Code:
    function bbCodeconverter($text){
      $search[0] = "/\[[bB]\](.*?)\[\/[bB]*\]/";
      $replace[0] = "<b>\\1</b>";
      $search[1] = "/\[img\](.*?)\[\/img]/";
      $replace[1] = "<img src=\"\\1\">";
    
      $text = preg_replace($search,$replace,$text);
      $text = str_replace("\n","<br>",$text);
      return $text;
    }
    As you can see, I modified the thingy to look for both the lowercase and uppercase version of b, using [bB]. I want to have the same using img and IMG. I tried using "/\[[img|IMG]\](.*?)\[\/[img|IMG]\]/", but that just doesn't work, all it does is splurting out the raw text I just sent to the replacerfunction.

    Can anyone help me? While your at it, how can I adjust the parser so it would look for strings like iMG, imG, ImG etc? Or can you only to that by entering all possible combinations manually in the $search[x] thingy?
     
  12. a1ehouse

    a1ehouse Chamone M*tha Fu*ka

    Messages:
    288
    Convert the case of the string as the first line:
    $text = strtolower($text)
    then look for everything in lowercase
     
  13. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    Yes, but that would make the complete text lowercase, wouldn't it? But I can convert it back using one of the other functions, but what if someone uses capatalization in the middle of the sentence (like 'I', an abbreviation, shouting etc), the system doesn't know what words are in caps.
    Is it possible to make only the converted text in lowercase, so it will keep the rest of the caps intact?
     
  14. a1ehouse

    a1ehouse Chamone M*tha Fu*ka

    Messages:
    288
    Use the "i" keyword to turn case sensitivity off:
    Code:
    <?php 
    $keyword='iMg'; 
    $string='img'; 
    $string = preg_replace('!('.$keyword.')!i','<span style="color:red">$1</span>',$string); 
    echo $string;
    ?>
    Or

    Code:
    <?php 
    echo sql_regcase("img"); 
    // [Ii][Mm][Gg] 
    ?>
     
  15. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    damn you, you beat me to the 'i' thing :p
    yeah just put the a lowercase i after your closing character for the preg

    ie "/\[B\]/i"

    anyways, please use <strong> instead of <b> for the HTML tag

    *I got beaten so had to offer up something ;)
     
  16. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    Thank you both :)
    @bunny: Why should I use <strong>? <b> does the same for me, and it is less typing :p
     
  17. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    <b> is depreciated and may be removed from the HTML spec sometime in the future

    <strong> for bold
    <em> for italics
     
  18. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    I don't know if you have the answer, but why? b=bold and i=italic, simple, right? Why do thay make it so difficult :(
     
  19. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    The adding of the 'i' was what I was looking for (I just tested it). Thanks :)
     
  20. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    And again, a question.
    I have this portion of search and replace:
    Code:
    $search[] = "/\[url\](.*?)\[\/url\]/i";
    $replace[] = "<a href=\"\\1\">\\1</a>";
    I have created this function to strip loooooong uri's (same thing this board does when you are posting something like [ url]http://loooooooooooooooooooooooooooooooooooooongurl.com[ /url], it's stripping the long text, but it's keeping the URI itself intact for proper linking). This is how the function looks like:
    Code:
    function strLongURI($uri){
      $uri_len = strlen($uri);
      if ($uri_len >= 15){
        $first_part = substr($uri,0,7);
        $second_part = substr($uri,-7);
        $str_uri = $first_part."..".$second_part;
      }else{
        $str_uri = $uri;
      }
      return $str_uri;
    }
    But how can I implement this in the $replace[] thingy? I have tried using $replace[] = "<a href=\"\\1\">".strLongURI('\\1')."</a>"; But that does nothing, it's just showing the full URI. How can I use functions in combination with these regular expressions?