G
G
Guran2014-02-03 06:42:24
PHP
Guran, 2014-02-03 06:42:24

Excluding an element by regexp

Good afternoon, in the course of digging one parser, a practical question arose.
Here is the regexp: /<strong>\s*(?:<img.*?\/?>)?(.*)<\/strong>/
(?:<img.*?\/?>)- this part means some image before the text. It may be, it may not be. If the image is missing, the text is parsed normally. But the problem is that if there is an image, it does not end up (?:<img.*?\/?>)here, but in the block with text (.*). How can I fix this regexp?
Parsing object example

<strong><img src="http://www.site.de/images/stories/galleries/reviews/ololo.jpg" alt="ololo" style="margin: 10px; float: left;" height="100" width="100" /> Пока космические корабли бороздят просторы северного ледовитого океана...</strong>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
PaulZi, 2014-02-03
@Guran

In php, everything seems to work as it should:
Maybe it's in flags, such as unicode (?u) or in text wrapping to a new line (?m).

array(2) {
  [0]=>
  string(301) "<strong><img src="http://www.site.de/images/stories/galleries/reviews/ololo.jpg" alt="ololo" style="margin: 10px; float: left;" height="100" width="100" /> Пока космические корабли бороздят просторы северного ледовитого океана...</strong>"
  [1]=>
  string(137) " Пока космические корабли бороздят просторы северного ледовитого океана..."
}

_
_ _, 2014-02-03
@AMar4enko

See rubular.com/r/PoWHYBdnL5

K
kompi, 2014-02-03
@kompi

Most likely you have problems with line breaks, check the /musi flags.
Remove non-capturing group ?:if you need img to be included in a separate result.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question