检查文字是否包含网址 [英] Check if text contains URL
本文介绍了检查文字是否包含网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查PHP中的文本中是否包含链接? 我的数据库表格式如下.
How can I check if text in PHP contains a link in it's text? I have a database table format as follow.
+------------------+
| id | posts | tag |
+------------------+
| 1 | text 1| 0 | //no links
| 2 | text 2| 1 | //contains links
基本上,我想验证提交的条目是否包含链接,如果确实存在,
tag
列的值为1
.
Basically, I want to verify that the submitted entry contains a link, if it does,
the tag
column will have a value of 1
.
有人可以帮助我正确编码上述示例吗?当前这是我的PHP:
Can someone help me to properly code the above sample? Currently this is my PHP:
include 'function.php';
$text= $_POST['text'];
//if $text contains a url then do this function
postEntryWithUrl($text);
//else here
postEntry($text);
推荐答案
您可以使用stristr()
$has_link = stristr($string, 'http://') ?: stristr($string, 'https://');
http://php.net/manual/en/function.stristr.php
或preg_match()
preg_match('/(http|ftp|mailto)/', $string, $matches);
var_dump($matches);
https://www.php.net/manual/zh/function.preg-match.php
这篇关于检查文字是否包含网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文