PHP正则表达式
一些链接如下:PHP正则表达式,php,regex,Php,Regex,一些链接如下: [links url='http://www.google.com.hk' title='Google' image='']google[/links] [links url='http://hk.yahoo.com' title='yahoo' image='']yahoo[/links] 如何使用PHP正则表达式获取url属性?谢谢 http://www.google.com.hk http://hk.yahoo.com 这应该让你开始: preg_match_all("
[links url='http://www.google.com.hk' title='Google' image='']google[/links]
[links url='http://hk.yahoo.com' title='yahoo' image='']yahoo[/links]
如何使用PHP正则表达式获取url属性?谢谢
http://www.google.com.hk
http://hk.yahoo.com
这应该让你开始:
preg_match_all("/\[links[^\]]+url='([^']+)'/", '{{your data}}', $arr, PREG_PATTERN_ORDER);
正则表达式的解释:
/
\[ //An excaped "[" to make it literal.
links //The work "links"
[^\]]+ //1+ non-closing brackent chars ([^] is a negative character class)
url=' //The work url='
([^']+) //The contents inside the '' in a caputuring group
/
使用此正则表达式:/\[links\s+?:[^\]*\s+*url=\'[^\']*\'[^\]*.]/
我可以建议在@Doug T.,不,url不保证,这些只是长文本,我只想得到url属性吗?@Doug T.我只知道简单的dom,但这不是常规链接。所以需要帮助。@fish如果你不懂regex,找人帮你做不会帮助你学习。可能想把这篇文章整合到preg_match_all:[[1]:事实上,如果url不是第一个,那就不起作用。试试看:[links title='Google'url='1]http://www.google.com.hk'image=]谷歌[/links]@雅各布:你说得对,我忘了一个*。我现在把它修好了,这样顺序就不重要了。
$str = "[links url='http://www.google.com.hk' title='Google' image='']google[/links]";
$m = array();
preg_match('/\[links\s+(?:[^\]]*\s+)*url=\'([^\']*)\'[^\]]*?\]/', $str, $m);
echo $m[1];