I need help understanding whether to use preg_replace or str_replace.

I have the following :

{list}

{}list item 1
{
}list item 2

{/list}

I need to replace{list} with the HTML <ul> tag
I need to replace {*} with the HTML <li> tag
I need to replace{/list} with the HTML </ul> tag

I've done a little research on both functions but don't know which to use - or really how to use them correctly here.

Any examples would be helpful

Thanks

    user [man]str_replace[/man]... [man]preg_replace[/man] or [man]ereg_replace[/man] are only good for patterns... if you know the exact string you want to replace each time than you dont need regular expressions

    to be nice and end your <li> with </li> you will need a regular expression... however this in not necessary

    read up on the functions... they have the ability to be used as i am using them here... compile a list of things you want to replace and then call the function once... both str and preg work the same in that respect

    <?php
    
    header('Content-type: text/plain');
    
    $orig_string = "
    {list}
    
    {*}list item 1
    {*}list item 2
    
    {/list} 
    ";
    
    /// the str method
    
    $str_find = array(
        '{list}',
        '{/list}',
        '{*}'
    );
    $str_replacement = array(
        '<ul>',
        '</ul>',
        '<li>'
    );
    
    $str_string = str_replace( $str_find, $str_replacement, $orig_string );
    
    /// the preg method
    
    $preg_find = array(
        '/\{list\}/',
        '/\{\/list\}/',
        '/\{\*\}([^\n]*)/'
    );
    $preg_replacement = array(
        '<ul>',
        '</ul>',
        '<li>\1</li>'
    );
    
    $preg_string = preg_replace( $preg_find, $preg_replacement, $orig_string );
    
    echo "
     ---------------- : original string :  ----------------
    ";
    echo $orig_string;
    echo "
     ---------------- : str_replace :  ----------------
    ";
    echo $str_string;
    echo "
     ---------------- : preg_replace :  ----------------
    ";
    echo $preg_string;
    
    ?>
    
    OUTPUT:
     ---------------- : original string :  ----------------
    
    {list}
    
    {*}list item 1
    {*}list item 2
    
    {/list} 
    
     ---------------- : str_replace :  ----------------
    
    <ul>
    
    <li>list item 1
    <li>list item 2
    
    </ul> 
    
     ---------------- : preg_replace :  ----------------
    
    <ul>
    
    <li>list item 1
    </li>
    <li>list item 2
    </li>
    
    </ul> 
    

      Thanks for the explaination ednark I will give them a try -

      I thought about using an array - but have read different things regarding the usage of preg and str replace.

      Another question I have is regarding the preg_replace -
      I have seen expressions with the /\/\/\ in them and $1 or $2 that seem to used as a delimeter of sorts - what's with the funky syntax?

      thanks

        Originally posted by mstringham
        Thanks for the explaination ednark I will give them a try -

        I thought about using an array - but have read different things regarding the usage of preg and str replace.

        Well, as ednark says, if you know the exact string you're trying to replace, str_replace() is perfectly adequate, and a lot faster (and less error-prone, because preg_replace() treats a lot of characters specially) than preg_replace.


        Another question I have is regarding the preg_replace -
        I have seen expressions with the /\/\/\ in them and $1 or $2 that seem to used as a delimeter of sorts - what's with the funky syntax?

        This is covered in the manual pages about preg_* functions (particularly the section on "Pattern Syntax". The /\/\/ you mention is commonly referred to as "falling toothpick syndrome" - since / has a special meaning in regexps, if you really want to say / (in URLs, for example), you have to escape it - and the escape character is a . Of course, that means \ is a special character as well...

        As for $1 and $2, those are back references (something that regular expressions aren't really supposed to be capable of - but this is programming, not language theory). If you wrap bits of your pattern in () then you can refer back to the strings those bits match with a $1 etc. (actually, \1 etc., but apparently $1 works as well) and say "... and then a repeat of that bit".

        A simple pattern, that finds bits of strings that are doubled up (in this sentence, it would find the double "t" in "pattern"). In the previous paragraph there is a double s, a double m, a double p, a double l, a double ., and in this paragraph there is a deliberate doubling deliberate doubling of the phrase "deliberate doubling":

        /(.+)\1/

          This code -
          $orig_string = "
          {list}

          {}list item 1
          {
          }list item 2

          {/list}
          ";

          /// the str method

          $str_find = array(
          '{list}',
          '{/list}',
          '{*}'
          );
          $str_replacement = array(
          '<ul>',
          '</ul>',
          '<li>'
          );

          $str_string = str_replace( $str_find, $str_replacement, $orig_string );

          }

          Returns
          {list} {}list item 1 {}list item 2 {/list}

          Am I doing something wrong here?

          Thanks

          MS

            should work

            1) \n characters: you should either do a header('Content-type: text/plain') at the beginning or wrap your prints in <pre></pre>
            so that \n get shown as newlines

            2) are you sure you are printing out $str_string and not $orig_string...

              Write a Reply...