我想知道是否可以检查文本框是否包含任何链接。如果可以在字段外选择该链接。
我这样试过,但问题是我不能选择整个链接,而且查询非常不精确。
$a = 'Lorem ipsum dolor sit amet, consetetur sadipscing www.loremipsum.com/asd/fgh/jkl elitr, sed diam ';
if (strpos($a, 'www.') !== false) {
echo 'true';
}
再次解释:
我有一个文本字段,其中可以包含评论和链接。我需要从此字段中选择正确的链接并将其单独保存在一个变量中。
我不想让你编写我的代码,但如果有人知道实现它的函数或方法,那就太好了。
谢谢!
最佳答案
这是一个匹配 www.whatever https://regex101.com/r/rdOKQB/1/ 的正则表达式
<?php
$string = 'Lorem ipsum dolor sit amet, consetetur sadipscing www.loremipsum.com/asd/fgh/jkl elitr, sed diam ';
$regex= '#www\.\S+#';
preg_match_all($regex,$string, $matches);
var_dump($matches);
哪些输出:www.loremipsum.com/asd/fgh/jkl
在这里查看:https://3v4l.org/4t2oV
您可能还想对 http://或 https://进行另一个正则表达式测试? https://regex101.com/r/rdOKQB/2
#https?:\/\/\S+#
关于javascript - PHP 检查文本框是否包含链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45982507/