Reg. Ex. Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ctiggerf

    Reg. Ex. Problem

    I am having a regular expression problem with a bit of code I'm working
    on. I was able to get a similar piece of code working in perl, but now
    I'm trying to convert it over. Basically what I'm trying to do here is
    some thing like this

    Get some html from an external file
    Look for my ids inside of the html, my ids look like {{###}}.
    Replace all the urls the ids are in with a url I get from my database
    matching the id.

    Here is a little sample code:

    <?php

    #some html - normally pulled from a text file on a server
    $html="Some <b>Stuff</bBefore the <a
    href='http://example.com'>Ex ample</a><br>\n- "
    ."<a href='http://example.com/example.php?id= {{111}}'>Exampl e
    1</a><br>\n- Now for 2:"
    ."<a href=\"http://example.com/example.php?id= {{2222}}\">Exam ple
    2</a><br>\n- Now for 3:"
    ."<a href='http://example.com/example.php?id= {{33}}&m=1'>Exa mple
    3</a><br>\n- Now for 4:"
    ."<a href=\"http://example.com/example.php?id= {{111}}\">Examp le
    4</a<br>\n- "
    ."Some <b>Stuff</bAfter the <a
    href='http://example.com'>Ex ample</a><br>";

    #Disply the Orignal
    echo "<textarea cols=100 rows=6>$html</textarea>";

    #Find all the id's
    preg_match_all( "/{{(\d+)}}/", $html, $matches, PREG_SET_ORDER) ;
    foreach($matche s as $match) {
    $id=$match[1];

    #the replace string - this would normally be pulled from a database
    matching the id
    $replace="http://example2.com/example2.php?id =$id";

    #the pattern
    $pattern="/href=('|\")[^\1]*?{{".$id."}}[^\1]*?\1/i";

    #print out what matches:
    echo "<br><texta rea cols=100 rows=4>$pattern \n";
    preg_match($pat tern, $html, $ms);
    print_r($ms);
    echo "</textarea>";

    #make the replacements:
    $html=preg_repl ace($pattern,$r eplace,$html);
    }

    #show the new html
    echo "<br><texta rea cols=100 rows=6>$html</textarea>";

    ?>

    I've tried just about everything I can think of in that second regular
    expression and can't get it to match. Any help you can give would be
    appreciated.

    Thank you,
    Chris.

  • ctiggerf

    #2
    Re: Reg. Ex. Problem


    ctiggerf wrote:
    I am having a regular expression problem with a bit of code I'm working
    on. I was able to get a similar piece of code working in perl, but now
    I'm trying to convert it over. Basically what I'm trying to do here is
    some thing like this
    >
    Get some html from an external file
    Look for my ids inside of the html, my ids look like {{###}}.
    Replace all the urls the ids are in with a url I get from my database
    matching the id.
    >
    Here is a little sample code:
    >
    <?php
    >
    #some html - normally pulled from a text file on a server
    $html="Some <b>Stuff</bBefore the <a
    href='http://example.com'>Ex ample</a><br>\n- "
    ."<a href='http://example.com/example.php?id= {{111}}'>Exampl e
    1</a><br>\n- Now for 2:"
    ."<a href=\"http://example.com/example.php?id= {{2222}}\">Exam ple
    2</a><br>\n- Now for 3:"
    ."<a href='http://example.com/example.php?id= {{33}}&m=1'>Exa mple
    3</a><br>\n- Now for 4:"
    ."<a href=\"http://example.com/example.php?id= {{111}}\">Examp le
    4</a<br>\n- "
    ."Some <b>Stuff</bAfter the <a
    href='http://example.com'>Ex ample</a><br>";
    >
    #Disply the Orignal
    echo "<textarea cols=100 rows=6>$html</textarea>";
    >
    #Find all the id's
    preg_match_all( "/{{(\d+)}}/", $html, $matches, PREG_SET_ORDER) ;
    foreach($matche s as $match) {
    $id=$match[1];
    >
    #the replace string - this would normally be pulled from a database
    matching the id
    $replace="http://example2.com/example2.php?id =$id";
    >
    #the pattern
    $pattern="/href=('|\")[^\1]*?{{".$id."}}[^\1]*?\1/i";
    >
    #print out what matches:
    echo "<br><texta rea cols=100 rows=4>$pattern \n";
    preg_match($pat tern, $html, $ms);
    print_r($ms);
    echo "</textarea>";
    >
    #make the replacements:
    $html=preg_repl ace($pattern,$r eplace,$html);
    }
    >
    #show the new html
    echo "<br><texta rea cols=100 rows=6>$html</textarea>";
    >
    ?>
    >
    I've tried just about everything I can think of in that second regular
    expression and can't get it to match. Any help you can give would be
    appreciated.
    >
    Thank you,
    Chris.

    Well, I spent something like 10 hrs farting with this before I posted.
    I guess I just needed 1 more hour after playing with it. The new
    pattern should be ..

    $pattern="/href=(\"|').*?{ {(".$id.")}}.*? \\1/i";

    There were a couple of problems from earlier .. the ones I know of are:


    \1 needed to be \\1 - Because you actually need the escape character in
    the regular expression.
    {{####}} needed to be {{(####)}} - Not exactly sure why, but my guess
    was that it thought {{111}} was a quantifier

    Perl wouldn't have had a problem with that because patterns are not
    enclosed in strings in perl.


    Thanks anyways if you started to look at it before I got this in.
    --Chris.

    Comment

    • Alvaro G. Vicario

      #3
      Re: Reg. Ex. Problem

      *** ctiggerf escribió/wrote (11 Aug 2006 10:41:02 -0700):
      Well, I spent something like 10 hrs farting with this before I posted.
      I guess I just needed 1 more hour after playing with it.
      That always happens to me too :)

      $pattern="/href=(\"|').*?{ {(".$id.")}}.*? \\1/i";
      {{####}} needed to be {{(####)}} - Not exactly sure why, but my guess
      was that it thought {{111}} was a quantifier
      Yep, { and } are meta-characters and should be escaped when not used as
      such:

      { = start min/max quantifier
      } = end min/max quantifier

      You have a full list in PHP manual:



      It should be:

      \{ and \}




      --
      -+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
      ++ Mi sitio sobre programación web: http://bits.demogracia.com
      +- Mi web de humor con rayos UVA: http://www.demogracia.com
      --

      Comment

      Working...