Home >Backend Development >PHP Tutorial >PHP matching character link address program code_PHP tutorial

PHP matching character link address program code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:081034browse

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
 代码如下 复制代码

$str="ssdsfsdfsdfss";
if(preg_match("/]*>|]*>/i",$str))
{
echo "该字符串有超链接";
}
else
{
echo "该字符串没有超链接标记";
}
?>

Copy code


$str="ssdsfsdfsdfss";
 代码如下 复制代码

echo preg_replace("/(?<=href=)([^>]*)(?=>)/i","#", "你好,点这里看看你好,点这里看看");
?>

if(preg_match("/]*>|]*>/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; }

www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn