在php中从textarea中分割url
我对正则表达式了解不多。我发现这段代码可以检测链接,或者您可以说插入在文本区域
$url = 'this is just a link http://stackoverflow.com/ to test';
$text = preg_replace("
#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie",
"'<a href=\"$1\" target=\"_blank\">$3</a>$4'",
$url
);
echo $text;
输出中写入的链接: 这只是一个要测试的链接 stackoverflow.com
但我需要将链接从该文本区域分离到一个变量中。例如变量 $var = http://stackoverflow.com 以便我可以使用该链接。
I don't have much knowledge about regular expression. I found this code to detect link or you can say to insert link that is written in textarea
$url = 'this is just a link http://stackoverflow.com/ to test';
$text = preg_replace("
#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie",
"'<a href=\"$1\" target=\"_blank\">$3</a>$4'",
$url
);
echo $text;
output:
this is just a link stackoverflow.com to test
But i need to separate the link into a variable from that text area. For example variable
$var = http://stackoverflow.com so that i can work with the link.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想捕获每个匹配并进行操作,可以使用 pre_replace_callback在替换之前进一步进行操作,或将其保存到其他地方。您也可以只存储该正则表达式并稍后进行匹配。我会使用前者,因为字符串中可能有多个链接,并且它比在匹配项上运行自己的 for 循环更干净。
You can use pre_replace_callback if you want to capture each match and manipulate further before replace, or save it elsewhere. You can also just store that regex and do a match later. I would use the former, since you may have more than one link in the string and it would be cleaner than running your own for loop over the matches.
您可以通过以下方式尝试:
You can try in this way:
尝试分两个步骤:
Try in two steps :