preg_match a hyperlink
I looked everywhere for an answer, but it seems I cannot get my head around preg_match
functionality. I want to preg_match
the link below where only the numbers part (the ID) is dynamic.
Link:
http://video.cnbc.com/gallery/?video=3000024508
Here goes what I have come up until now:
preg_match( '/^http://video.cnbc.com/gallery/?video=([0-9_-]/', $content )
开发者_开发百科
But it won't work.
preg_match('#^http://video\.cnbc\.com/gallery/\?video=([0-9_-]+)$#', $content);
RegExr Demo
Besides problems that others mentioned (escaping the ?
and .
and using #
instead of /
) you are also missing a +
from after the number group ([0-9_-]
), which means that group can be repeated.
If you need to check if a string includes this kind of link or not, remove ^
and $
:
preg_match('#http://video\.cnbc\.com/gallery/\?video=([0-9_-]+)#', $content);
Well, you're missing a close paren after [0-9_-] and you didn't escape properly. Use # instead of / and you'll get this:
preg_match( '#^http://video\.cnbc\.com/gallery/\?video=([0-9_-]+)#', $content )
You didn't escape it properly:
preg_match( '/^http:\/\/video.cnbc.com\/gallery\/\?video=([0-9_-])/', $content)
Here are the problem characters:
/
means start and end of regex, you were ending it afterhttp:
?
means the previous character is optional.
You also forgot a closing parenthesis.
精彩评论