PHP Regular express problem

4 replies
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
#express #php #problem #regular
  • Profile picture of the author Spencer Westwood
    Yes its the line break.. add a \n\l to the match list and see if that helps..
    {{ DiscussionBoard.errors[765773].message }}
  • Profile picture of the author xga
    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
    {{ DiscussionBoard.errors[768633].message }}
    • Profile picture of the author xiaophil
      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);
      {{ DiscussionBoard.errors[771509].message }}
      • Profile picture of the author Aaron Sustar
        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];
        {{ DiscussionBoard.errors[777695].message }}

Trending Topics