PHP Regular express problem

by 4 replies
5
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
#programming #express #php #problem #regular
  • Yes its the line break.. add a \n\l to the match list and see if that helps..
  • 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
    • [1] reply
    • 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);
      • [1] reply

Next Topics on Trending Feed