Welcome to WebmasterWorld Guest from 54.161.175.236

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

why this regex doesn't work?

     
7:15 pm on Mar 24, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2007
posts:147
votes: 0


Hi, I'm trying to grab the content like this with preg_match or eregi_replace:

The content is like this:
<span class=a>.....</span>
<a class=fl href="www.example.com">....</a>
<table>....</table>
<a class=fl href="www.example.com">....</a>

The regex i wrote:
<span class=a>(.*?)</span><a class=fl(.*?)</a>(.*?)<a class=fl(.*?)</a>

By using regex verifier software, the regex i use seems to be valid, but after executing it, it says "Unknown modifier".

Do you have any idea what happens? I've spent an hour on this, but still have no idea. Any help is highly appreciated. Thanks thanks!

10:36 pm on Mar 24, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


What is the exact php code you are using with the regex--more specifically, the code for the pattern?
12:41 pm on Mar 26, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2007
posts:147
votes: 0


hi, i tried both preg_replace and eregi_replace before. The code is like this:

$output = eregi_replace("<span class=a>(.*?)</span><a class=fl(.*?)</a>(.*?)<a class=fl(.*?)</a>", "hello word", $output);

and...
$output = preg_replace("/<span class=a>(.*?)</span><a class=fl(.*?)</a>(.*?)<a class=fl(.*?)</a>/is", "hello world", $output);

but it seems both sentences doesn't work too. very frustrated.....

1:15 pm on Mar 26, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Dec 3, 2006
posts:257
votes: 0


Several remarks here:
* in preg_replace() you must escape the "/" char in the closing html tags : <\/span>
* in the example text you have a "space" between the </span> and the next <a >. The correct RE for preg_replace looks like this: "/<span class=a>(.*?)<\/span>\s*<a class=fl(.*?)<\/a>(.*?)<a class=fl(.*?)<\/a>/is"
* The way the replace function work, it will replace the whole RE with "hello world". Not the (.*?).

If you need to extract some information from the code, you should use preg_match() or preg_match_all(), and work with the "matches" array.

7:30 am on Apr 4, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2007
posts:147
votes: 0


thanks a lot, achernar! I'll be more careful next time on regex. I really got mad with this.