php 正则函数preg_replace_callback

$str = '<p>在有意思了!</p><p><img src="https://taoke168.oss-cn-beijing.aliyuncs.com/uploads/20200529/d023b6c447ac4c77274a29f0bd2791e5.png" data-filename="filename" style="width: 471px;"><br></p>';
//preg_match( '/src\s*=\s*(\'|\")(.*?)(\'|\")/i' , $str , $match );
//print_r($match);
function next_replace($matches){
    print_r($matches);
    $matches[4] = '111.jpg';
  return $matches[1].$matches[2].$matches[3].$matches[4].$matches[5]; //替换匹配的字符串
}
//
echo preg_replace_callback(
    '/(src)\s*(=)\s*(\'|\")(.*?)(\'|\")/i',
    "next_replace",
    $str);

 

posted on 2022-03-25 21:15  running-fly  阅读(48)  评论(0编辑  收藏  举报

导航