PHP检查文本框是否包含链接

时间:2017-08-31 13:21:27

标签: javascript php string codeigniter variables

我想知道是否可以检查文本框是否包含任何链接。如果可以从该字段中选择该链接。

我试过这样,但问题是我无法选择整个链接而且查询非常不精确。

$a = 'Lorem ipsum dolor sit amet, consetetur sadipscing www.loremipsum.com/asd/fgh/jkl elitr, sed diam ';

                if (strpos($a, 'www.') !== false) {
                    echo 'true';
                }

再说一遍:

我有一个文本字段,可以包含注释,但也包含链接。我需要从该字段中选择正确的链接,并在变量中单独保护它。

我不希望你对我的代码进行编码,但如果有人知道一个函数或方法来实现这一点,那将会很棒。

谢谢!

2 个答案:

答案 0 :(得分:2)

这是一个匹配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+#

答案 1 :(得分:0)

您可以使用strstr返回下一个单词而不是strpos。

相关问题