[英]PHP Check if textbox contains a link
I want to know if it is possible to check if a textbox contains any link. 我想知道是否可以检查文本框是否包含任何链接。 And if it is possible to select that link out of the field.
并且,如果可以在字段外选择该链接。
I tried it like this, but the problem is that i can't select the whole link and the query is very imprecise. 我是这样尝试的,但是问题是我无法选择整个链接,查询非常不精确。
$a = 'Lorem ipsum dolor sit amet, consetetur sadipscing www.loremipsum.com/asd/fgh/jkl elitr, sed diam ';
if (strpos($a, 'www.') !== false) {
echo 'true';
}
So again for explanation: 再次解释一下:
I have a text field which can contain a comment but also links. 我有一个文本字段,可以包含注释,也可以包含链接。 I need to select the correct link out of this field and safe it seperatly in a variable.
我需要从该字段中选择正确的链接,并将其单独保存在变量中。
I don't want you to code my code but it would be great if anyone know a function or a method to realize this. 我不想让您编写代码,但是如果有人知道实现此功能的函数或方法,那将是很好的。
Thank you! 谢谢!
Here's a regex that matches www.whatever https://regex101.com/r/rdOKQB/1/ 这是一个与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);
Which outputs : www.loremipsum.com/asd/fgh/jkl
See it here: https://3v4l.org/4t2oV 哪个输出:
www.loremipsum.com/asd/fgh/jkl
在此处查看: https : www.loremipsum.com/asd/fgh/jkl
You might want another regex testing for http:// or https:// instead/too? 您可能还想针对http://或https://进行其他正则表达式测试吗? https://regex101.com/r/rdOKQB/2
https://regex101.com/r/rdOKQB/2
#https?:\/\/\S+#
代替strpos,您可以使用strstr返回下一个单词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.