homepage Welcome to WebmasterWorld Guest from 54.237.213.31
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
why this regex doesn't work?
zozzen

5+ Year Member



 
Msg#: 3609369 posted 7:15 pm on Mar 24, 2008 (gmt 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!

 

eelixduppy

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



 
Msg#: 3609369 posted 10:36 pm on Mar 24, 2008 (gmt 0)

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

zozzen

5+ Year Member



 
Msg#: 3609369 posted 12:41 pm on Mar 26, 2008 (gmt 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.....

Achernar

5+ Year Member



 
Msg#: 3609369 posted 1:15 pm on Mar 26, 2008 (gmt 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.

zozzen

5+ Year Member



 
Msg#: 3609369 posted 7:30 am on Apr 4, 2008 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved