Why does preg_match doesn't find a string between tags when regex is OK?

yuri kroz

I have the next issue here. I'm trying to fetch a string between two HTML comment tags with preg_match/preg_match_all but the result I'm getting is empty array with a lot of records.

Here's my code:

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

The regular expression pattern itself seems to be valid since it works for me when I put just a short sample string (like: "123456") between two tags within $description variable.

However on the real life content it doesn't work and my guess is that this happens because of the content's length.

Here is the example of the real case the preg_match fails for me: http://www.phpliveregex.com/p/6oJ

Could anyone explain please how the issue could be solved? It's always possible to play with simple string functions like strpos, substr and etc. but isn't there other better options?

Thank you very much!

Toni Perić

You should add the "matches newline" flag, which is s.

So your regex should be the following:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find specific string by regex preg_match

From Dev

How to find specific string by regex preg_match

From Dev

Why doesn't this regex match if a parallel of it does?

From Dev

Why doesn't this simple PHP preg_match() function work?

From Dev

preg_match or preg_match_all to find all words in string with anything between them

From Dev

PHP preg_match doesn't match

From Dev

PHP preg_match doesn't match

From Dev

Get String between to string preg_match

From Dev

Match regex that doesn't contain pair of tags

From Dev

Why doesn't the regex ^([0|1]1)+$ match the string "111"?

From Dev

regex in C doesn't match when egrep does

From Dev

Why doesn't this regex match?

From Dev

checking returns true when string doesn't even match (RegEx)

From Dev

Why does regex not match this string

From Dev

Preg_match with Cyrillic doesn't work

From Dev

Preg_match with Cyrillic doesn't work

From Dev

Preg_match doesn't work right

From Dev

Find: why the regex with egrep doesn't work?

From Dev

I can't get preg_match to test if the entire string matches the regex

From Dev

preg_match doesn't behave as expected when validating windows filesystem path

From Dev

How to find some string by regex but when special string doesn't adjoin?

From Dev

How to find some string by regex but when special string doesn't adjoin?

From Java

Why can't I find this string in RegEx?

From Dev

Regex match a string that doesn't contains a string

From Dev

Preg_match string inside curly braces tags

From Dev

regex doesn't find a match if it is defined in a function

From Dev

PHP preg_match doesn't match list separated with commas

From Dev

Preg_match (REGEX) -- how to differentiate between accredited and credit?

From Dev

regex/preg_match alternating between numeric and alpha separated by space

Related Related

  1. 1

    How to find specific string by regex preg_match

  2. 2

    How to find specific string by regex preg_match

  3. 3

    Why doesn't this regex match if a parallel of it does?

  4. 4

    Why doesn't this simple PHP preg_match() function work?

  5. 5

    preg_match or preg_match_all to find all words in string with anything between them

  6. 6

    PHP preg_match doesn't match

  7. 7

    PHP preg_match doesn't match

  8. 8

    Get String between to string preg_match

  9. 9

    Match regex that doesn't contain pair of tags

  10. 10

    Why doesn't the regex ^([0|1]1)+$ match the string "111"?

  11. 11

    regex in C doesn't match when egrep does

  12. 12

    Why doesn't this regex match?

  13. 13

    checking returns true when string doesn't even match (RegEx)

  14. 14

    Why does regex not match this string

  15. 15

    Preg_match with Cyrillic doesn't work

  16. 16

    Preg_match with Cyrillic doesn't work

  17. 17

    Preg_match doesn't work right

  18. 18

    Find: why the regex with egrep doesn't work?

  19. 19

    I can't get preg_match to test if the entire string matches the regex

  20. 20

    preg_match doesn't behave as expected when validating windows filesystem path

  21. 21

    How to find some string by regex but when special string doesn't adjoin?

  22. 22

    How to find some string by regex but when special string doesn't adjoin?

  23. 23

    Why can't I find this string in RegEx?

  24. 24

    Regex match a string that doesn't contains a string

  25. 25

    Preg_match string inside curly braces tags

  26. 26

    regex doesn't find a match if it is defined in a function

  27. 27

    PHP preg_match doesn't match list separated with commas

  28. 28

    Preg_match (REGEX) -- how to differentiate between accredited and credit?

  29. 29

    regex/preg_match alternating between numeric and alpha separated by space

HotTag

Archive