Can't figure out why OR condition in my regexp isn't working.
$str = " <li><a href=\"\" class=\"gateway-image\"><img src=\"<?=theme_url()?>/images/Cherry Credits.jpg\" alt=\"\"/></a></li>
<li><a href=\"\" class=\"gateway-image\"><img src=\"<?=theme_url()?>/images/gudangvoucher.jpeg\" alt=\"\" style=\"width: 140px!important\"/></a></li>";
$regexp = '/^(.*\<li\>\<a href\=)(.*cherry|credits.*)$/im';
preg_match($regexp, $str, $m);
It seems regexp is correct but it can't find string Cherry Credits.jpg
as I expected. What is wrong?
You've written the expression using the start- and end-of-line anchors ^
and $
.
This doesn't match because your string continues on after cherry
and there are additional characters between href=
and credits
.
You can either remove the line anchors:
(.*\<li\>\<a href\=)(.*cherry|credits.*)
Or you can move the trailing .*
(and probably the leading .*
) outside of the capture group:
^(.*\<li\>\<a href\=).*(cherry|credits).*$
You need to remove the "$" sign at the end, because it will expect a end of line.
Try this:
^(.*\\<li\\>\\<a href\\=).*(cherry|credits)(\\.jpg)
You can use online testers like this site: https://regex101.com/ and check if you are doing OK.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.