Reg. Ex. Problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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:


#some html - normally pulled from a text file on a server
$html="Some <b>Stuff</b> Before the <a
href=' '>Example</a><br>\n- "
  ."<a href=' =}'>Example
1</a><br>\n- Now for 2:"
  ."<a href=\" =}\">Example
2</a><br>\n- Now for 3:"
  ."<a href=' =}&m=1'>Example
3</a><br>\n- Now for 4:"
  ."<a href=\" =}\">Example
4</a<br>\n- "
  ."Some <b>Stuff</b> After the <a
href=' '>Example</a><br>";

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

#Find all the id's
preg_match_all("/}/", $html, $matches, PREG_SET_ORDER);
foreach($matches as $match) {

  #the replace string - this would normally be pulled from a database
matching the id
  $replace=" =$id";

  #the pattern

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

  #make the replacements:

#show the new html
echo "<br><textarea 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

Thank you,

Re: Reg. Ex. Problem

ctiggerf wrote:
Quoted text here. Click to load it

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 ..


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

needed to be - 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 } 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.

Re: Reg. Ex. Problem

*** ctiggerf escribió/wrote (11 Aug 2006 10:41:02 -0700):
Quoted text here. Click to load it

That always happens to me too :)

Quoted text here. Click to load it

Yep, { and } are meta-characters and should be escaped when not used as

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

You have a full list in PHP manual:

It should be:

    \{ and \}

-+ - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web:
+- Mi web de humor con rayos UVA:

Site Timeline