Php 用锚定标签替换所有img标签

Php 用锚定标签替换所有img标签,php,preg-replace-callback,Php,Preg Replace Callback,将所有img标记替换为锚定标记,其中img src属性值应为锚定标记href属性值。我不知道如何编写模式来匹配整体,替换img标记,并在下面的流程函数中使用href值作为img标记的src属性值返回锚标记 我试过以下方法: $pattern = '/<img[^>]+>/i'; $callback_fn = 'process'; $content = preg_replace_callback($pattern, $callback_fn, $string); func

将所有img标记替换为锚定标记,其中img src属性值应为锚定标记href属性值。我不知道如何编写模式来匹配整体,替换img标记,并在下面的流程函数中使用href值作为img标记的src属性值返回锚标记

我试过以下方法:

$pattern = '/<img[^>]+>/i'; 
$callback_fn = 'process';   
$content = preg_replace_callback($pattern, $callback_fn, $string);
function process($matches) 
{
        print_r($matches);
    return "&nbsp;&nbsp;<a href='http://mywebsite.com/".$matches[0]."'> <font color ='black' >View Image</font>&nbsp;&nbsp;</a>";
}       
echo $content;
在这里,查看图像与

this is dummy string View Image this is another sentesnces View Image this is third one
像这样试试

http://mywebsite.com/imageone.jpg
$pattern='/]*>/iU';
$callback_fn='process';
$string='这是伪字符串,这是另一个句子,这是第三个';
$content=preg\u replace\u callback($pattern,$callback\u fn,$string);
函数过程($matches)
{
返回“”;
}
echo$内容;
另外,不要使用
标记,而是使用
,因为
已被弃用。

尝试这样做

http://mywebsite.com/imageone.jpg
$pattern='/]*>/iU';
$callback_fn='process';
$string='这是伪字符串,这是另一个句子,这是第三个';
$content=preg\u replace\u callback($pattern,$callback\u fn,$string);
函数过程($matches)
{
返回“”;
}
echo$内容;

另外,不要使用
标记,而是使用
,因为
已被弃用。

不要手动执行此操作,请使用更适合您工作的工具:查看phps DOM扩展。否则,您将为img标记使用所有不同的编码选项而陷入地狱……不要手动操作,请使用更适合您工作的工具:看看phpsdom扩展。否则,你将陷入地狱与所有不同的编码选项的img标签。。。
http://mywebsite.com/imageone.jpg
$pattern = '/<img.+src=(.)(.*)\1[^>]*>/iU';
$callback_fn = 'process';
$string = 'this is dummy string <img src="imageone.jpg" alt="" /> this is another sentesnces <img src="imagetwo.jpg" /> this is third one';
$content = preg_replace_callback($pattern, $callback_fn, $string);
function process($matches)
{
    return "&nbsp;&nbsp;<a href='http://mywebsite.com/".$matches[2]."'> <font color ='black' >View Image</font>&nbsp;&nbsp;</a>";
}
echo $content;