Why doesn't preg_match find the string between tags in regex?

I have the following problem here. I'm trying to get the string between two HTML comment tags with preg_match / preg_match_all, but the result I get is an empty array with a lot of entries.

Here's my code:

$pattern = "/\<!-- Start of NewsTicker --\>(.*)\<!-- End of NewsTicker --\>/";
preg_match_all($pattern, $description , $matches);

      

The regex pattern itself seems to be valid as it works for me when I only put a short pattern string (ex: "123456") between the two tags in the $ description variable.

However, in real life content this does not work and I am guessing it is due to the length of the content.

Here's an example of a real case where preg_match doesn't work for me: http://www.phpliveregex.com/p/6oJ

Can anyone please explain how to solve this problem? It's always possible to play with simple string functions like strpos, substr, etc., but aren't there any other better options?

Many thanks!

+3


source to share


2 answers


You must add the "match newline" flag, which is s

.

So your regex should be like this:



$pattern = "/\<!-- Start of NewsTicker --\>(.*)\<!-- End of NewsTicker--\>/s";

      

+3


source


Have you tried escaping your backslashes and other special characters? Also, it's usually best to use single quotes when working with preformatted text or RegEx.

So:



$pattern = '/\\<\!-- Start of NewsTicker --\\>(.*)\\<\!-- End of NewsTicker --\\>/'
preg_match_all($pattern, $description , $matches);

      

Also useful: overapi.com/regex/

0


source







All Articles