PHP移除字符串中URL地址

PHP 投稿 47600 0 评论

PHP移除字符串中URL地址

PHP过滤字符串中URL地址

$content='测试字符串:http://www.feishuai.vip非常好。';
$content = preg_replace('/\<a.*?\>.*?<\/a>/i', '', $content);
$pattern = '`.*?((http|https)://[\w#$&+,\/:;=?@.-]+)[^\w#$&+,\/:;=?@.-]*?`i';
if (preg_match_all($pattern, $content, $matches)) {
    if (preg_match_all("/<img[^<>]*src=[\"]([^\"]+)[\"][^<>]*>/im", $content, $imgMatches)) {
        foreach ($matches[1] as $key => $url) {
            if (in_array($url, $imgMatches[1])) {
                unset($matches[1][$key]);
            }
        }
    }
    $content = str_ireplace($matches[1], '', $content);
}
var_dump($content);

编程笔记 » PHP移除字符串中URL地址

赞同 (51) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽