Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   PHP Regular express problem (https://www.warriorforum.com/programming/81958-php-regular-express-problem.html)

edpudol1973 7th May 2009 10:57 PM

PHP Regular express problem
 
Please help I am working for more than 3 hours now this problem but I can't think or find way to correct it...

my regular expression is like this
%<h[^>]+>(.*)</h.>%i

It works perfectly if the string look like this
Code:

<H1><A NAME="SECTION000300000000000000000">2 Simple Patterns</A></H1>
Result : <A NAME="SECTION000300000000000000000">2 Simple Patterns</A></H1>
But if string will look like this
Code:

<H1><A NAME="SECTION000300000000000000000">2 Simple Patterns
</A>
</H1>

Result : None

I guess the problem is the line break or not sure..

Thanks in advance

Spencer Westwood 8th May 2009 05:16 AM

Re: PHP Regular express problem
 
Yes its the line break.. add a \n\l to the match list and see if that helps..

xga 9th May 2009 03:02 AM

Re: PHP Regular express problem
 
Use the 's' pattern modifier so that the (.*) will also match any newlines in the string. For more details, you can read this: PHP: Possible modifiers in regex patterns - Manual

xiaophil 10th May 2009 10:30 AM

Re: PHP Regular express problem
 
Hi, maybe you could just strip out all the newlines before applying your regex, as they have no meaning in HTML anyway.

$string = str_replace("\n", "", $string);
$string = str_replace("\r", "", $string);

Aaron Sustar 12th May 2009 12:03 PM

Re: PHP Regular express problem
 
I would do something like this:

$string = str_replace(array("\n", "\r"), array("", ""), $string);

and then use the following code:

preg_match("#<h[^>]+>(.*)</h.>#i", $string, $match);
$your_result = $match[1];


All times are GMT -6. The time now is 02:43 PM.