Php 正则表达式删除图像周围的链接

Php 正则表达式删除图像周围的链接,php,regex,preg-replace,Php,Regex,Preg Replace,我有一页用链接包装的图片。基本上,我想删除图像周围的链接,但要保持图像标签的圆滑 我有: <a href="something.html" alt="blah"><img src="image1.jpg" alt="image 1"></a> 我想: <img src="image1.jpg" alt="image 1"> 我尝试了我在研究中发现的这个代码,但它留下了一个错误的标签 $content = preg_replace

我有一页用链接包装的图片。基本上,我想删除图像周围的链接,但要保持图像标签的圆滑

我有:

<a href="something.html" alt="blah"><img src="image1.jpg" alt="image 1"></a>

我想:

<img src="image1.jpg" alt="image 1">

我尝试了我在研究中发现的这个代码,但它留下了一个错误的标签

$content =
    preg_replace(
        array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
            '{ wp-image-[0-9]*" ></a>}'),
        array('<img','" />'),
        $content
    );
$content=
预更换(
数组(“{]*>”),
$content
);

我不知道什么时候会用到正则表达式,所以有人可以修复我的代码吗?:-)

通过您提供的正则表达式,您似乎正在使用wordpress并希望从内容中删除超链接

add_filter( 'the_content', 'attachment_image_link_remove_filter' );

function attachment_image_link_remove_filter( $content ) {
    $content =
        preg_replace(
            array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
                '{ wp-image-[0-9]*" /></a>}'),
            array('<img','" />'),
            $content
        );
    return $content;
}
如果您使用的是wordpress,那么您也可以使用此挂钩从内容中删除图像上的超链接

add_filter( 'the_content', 'attachment_image_link_remove_filter' );

function attachment_image_link_remove_filter( $content ) {
    $content =
        preg_replace(
            array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
                '{ wp-image-[0-9]*" /></a>}'),
            array('<img','" />'),
            $content
        );
    return $content;
}
add_filter('the_content'、'attachment_image_link_remove_filter');
函数附件\图像\链接\删除\过滤器($content){
$content=
预更换(
数组('{

$string='';
$result=preg_replace(“/(.*?/”,“\\2”,$string);
echo$result;//这将输出“”

根据您提供的正则表达式,您似乎正在使用wordpress并希望从内容中删除超链接

add_filter( 'the_content', 'attachment_image_link_remove_filter' );

function attachment_image_link_remove_filter( $content ) {
    $content =
        preg_replace(
            array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
                '{ wp-image-[0-9]*" /></a>}'),
            array('<img','" />'),
            $content
        );
    return $content;
}
如果您使用的是wordpress,那么您也可以使用此挂钩从内容中删除图像上的超链接

add_filter( 'the_content', 'attachment_image_link_remove_filter' );

function attachment_image_link_remove_filter( $content ) {
    $content =
        preg_replace(
            array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
                '{ wp-image-[0-9]*" /></a>}'),
            array('<img','" />'),
            $content
        );
    return $content;
}
add_filter('the_content'、'attachment_image_link_remove_filter');
函数附件\图像\链接\删除\过滤器($content){
$content=
预更换(
数组('{

$string='';
$result=preg_replace(“/(.*?/”,“\\2”,$string);
echo$result;//这将输出“”
您可以使用

$content=preg\u replace('/您可以使用


$content=preg\u replace(“/你在使用wordpress吗?你在使用wordpress吗?是的。正是我在做的。你的解决方案似乎仍然保留了那个错误的链接关闭标记…是的。正是我在做的。你的解决方案似乎仍然保留了那个错误的链接关闭标记…嗯。似乎不喜欢那个。给我一个警告:“没有结尾匹配分隔符”>”@henZa忘记了正则表达式的
/…/
。请检查更新的答案:)嗯。似乎不喜欢那个答案。给我一个警告:“没有结尾匹配分隔符”>。@henZa忘记了正则表达式的
/…/
。请检查更新的答案:)