Yesterday, a friend asked me whether it is possible to check whether the data submitted by the user includes super connections. If so, it will be filtered directly. Let me introduce to you how to filter the connections.
Determine whether a string contains a hyperlink
The code is as follows
|
Copy code
|
$str="ssdsfsdfsdfss";
代码如下 |
复制代码 |
echo preg_replace("/(?<=href=)([^>]*)(?=>)/i","#", "你好,点这里看看你好,点这里看看");
?>
|
if(preg_match("/]*>|/[^a]*a[^>]*>/i",$str))
{
echo "This string has a hyperlink";
}
else
{
echo "The string has no hyperlink tag";
}
?>
Next we only need to filter the connection part.
代码如下 |
复制代码 |
function match_links($document) {
preg_match_all("']+))[^>]*>?(.*?)'isx",$document,$links);
while(list($key,$val) = each($links[2])) {
if(!empty($val))
$match['link'][] = $val;
}
while(list($key,$val) = each($links[3])) {
if(!empty($val))
$match['link'][] = $val;
}
while(list($key,$val) = each($links[4])) {
if(!empty($val))
$match['content'][] = $val;
}
while(list($key,$val) = each($links[0])) {
if(!empty($val))
$match['all'][] = $val;
}
return $match;
}
|
The code is as follows |
Copy code |
|
echo preg_replace("/(?<=href=)([^>]*)(?=>)/i","#", " Hello, click here to take a lookHello, click here to take a look");
?>
Regular: /(?<=href=)([^>]*)(?=>)/
(?<=exp) matches the position after exp <🎜>
(?=exp) matches the position before exp <🎜>
This regular match matches all characters <🎜> that are not “>” after href= and before “>”
Example:
Find these characters (url) and replace them with # to remove all links in the html.
Now share an example of extracting hyperlinks
The code is as follows
|
Copy code
|
function match_links($document) {
preg_match_all("']+))[^ >]*>?(.*?)'isx",$document,$links);
while(list($key,$val) = each($links[2])) {
if(!empty($val))
$match['link'][] = $val;
}
while(list($key,$val) = each($links[3])) {
if(!empty($val))
$match['link'][] = $val;
}
while(list($key,$val) = each($links[4])) {
if(!empty($val))
$match['content'][] = $val;
}
while(list($key,$val) = each($links[0])) {
if(!empty($val))
$match['all'][] = $val;
}
return $match;
}
http://www.bkjia.com/PHPjc/631563.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631563.htmlTechArticleYesterday, a friend asked whether it is possible to check whether the data submitted by the user includes hyperlinks. If so, Filtered directly, let me introduce to you how to filter connections...
|
|