找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索本站精品资源

首页 教程频道 php教程 查看内容

php如何替换img中src内容-PHP问题

作者:模板之家 2020-10-28 16:34 1978人关注

php替换img中src内容的方法是:可以通过preg_replace()函数来实现。该函数用于执行一个正则表达式的搜索和替换。具体使用方法:【preg_replace($rule,imgsrc,$content)】。,php如何替换img中src内容-PHP问题

php替换img中src内容的方法是:可以通过preg_replace()函数来实现。该函数用于执行一个正则表达式的搜索和替换。具体使用方法:【preg_replace($rule,'

函数介绍:

(推荐教程:php教程)

preg_replace 函数执行一个正则表达式的搜索和替换。

函数语法:

mixed preg_replace(mixed $pattern, mixed $replacement, mixed $subject[, int $limit = -1[, int &$count]])

搜索 subject 中匹配 pattern 的部分, 以 replacement 进行替换。

参数说明:

  • $pattern: 要搜索的模式,可以是字符串或一个字符串数组。

  • $replacement: 用于替换的字符串或字符串数组。

  • $subject: 要搜索替换的目标字符串或字符串数组。

  • $limit: 可选,对于每个模式用于每个 subject 字符串的最大可替换次数。 默认是-1(无限制)。

  • $count: 可选,为替换执行的次数。

返回值:

如果 subject 是一个数组, preg_replace() 返回一个数组, 其他情况下返回一个字符串。

如果匹配被查找到,替换后的 subject 被返回,其他情况下 返回没有改变的 subject。如果发生错误,返回 NULL。

代码实现:

/**
* 图片地址替换成压缩URL
* @param string $content 内容
* @param string $suffix 后缀
*/
function get_img_thumb_url($content="",$suffix="/statics/images/product_2.jpg")
{
    $pregRule = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp]))[\'|\"].*?[\/]?>/";
    $content = preg_replace($pregRule, '', $content);
    return $content;
}

以上就是php如何替换img中src内容的详细内容,更多请关注 模板之家(www.mb5.com.cn) 其它相关文章!


路过

雷人

握手

鲜花

鸡蛋
原作者: 互联网 来自: 网络收集

全部回复(0)