Welcome to WebmasterWorld Guest from 54.167.76.176

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

why this regex doesn't work?

     

zozzen

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

5+ Year Member



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!

eelixduppy

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

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



What is the exact php code you are using with the regex--more specifically, the code for the pattern?

zozzen

12:41 pm on Mar 26, 2008 (gmt 0)

5+ Year Member



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

Achernar

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

5+ Year Member



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.

zozzen

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

5+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month