【问题标题】:preg_match links in text [duplicate]文本中的 preg_match 链接[重复]
【发布时间】:2018-04-18 12:18:53
【问题描述】:

我需要 preg_match html 来添加文本链接。目前使用:

'![a-z][a-z-]+://!i'

例子:

some text mylink.com
Become
some text <a href="mylink.com">mylink.com</a>

没关系。但是我在第一个代码中已经有了“a href”,需要跳过。我需要更改此 preg_match 以检测与第一个空白空间的链接。也许:

'!^[a-z][a-z-]+://!i'

但这不起作用,有什么帮助吗?

【问题讨论】:

标签: php preg-match


【解决方案1】:
'/<a\s+(?:[^"'>]+|"[^"]*"|'[^']*')*href=("[^"]+"|'[^']+'|[^<>\s]+)/i'

或者你使用 simpledom praser => http://simplehtmldom.sourceforge.net/

//from string
$html = str_get_html($links);

//or from webpage
$html = file_get_html('www.example.com');

foreach($html->find('a') as $link) {
    echo $link->href . '<br />';
}

【讨论】:

    猜你喜欢
    • 2012-03-10
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多