正则表达式删除图像周围的链接
Regex to remove links surrounding images
我有一页包含链接的图片。本质上我想删除图像周围的链接,但保持图像标签完好无损。
例如。我有:
<a href="something.html" alt="blah"><img src="image1.jpg" alt="image 1"></a>
我想要:
<img src="image1.jpg" alt="image 1">
我尝试了我在研究中发现的这段代码,但它留下了一个杂散的 </a>
标签。
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
'{ wp-image-[0-9]*" ></a>}'),
array('<img','" />'),
$content
);
我对正则表达式一窍不通,请问有人可以修改我的代码吗? :-)
根据您提供的正则表达式,您似乎正在使用 wordpress 并希望从内容中删除超链接。
如果您使用的是 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;
}
这是另一个同样有效的函数。
function attachment_image_link_remove_filter($content)
{
$content =
preg_replace(array('{<a[^>]*><img}', '{/></a>}'), array('<img', '/>'), $content);
return $content;
}
add_filter('the_content', 'attachment_image_link_remove_filter');
或者您也可以使用 Demo
$string = '<a href="something.html" alt="blah"><img src="image1.jpg" alt="image 1"></a>';
$result = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\2", $string);
echo $result; // this will output "<img src="image1.jpg" alt="image 1">"
可以用<a.*?(<img.*?>)<\/a>
来匹配替换成</code></p>
<p>见<a href="https://regex101.com/r/xX9pJ8/1" rel="nofollow">DEMO</a></p>
<pre><code>$content = preg_replace('/<a.*?(<img.*?>)<\/a>/', '', $content);
我认为
$content = preg_replace('/<a\s+href=[^>]+>(<img[^>]+>)<\/a>/', '', $content);
是更好的解决方案。
例如:https://regex101.com/r/3ozruM/1
因为@karthik manchala 的解决方案https://regex101.com/r/ETkE58/1 在这种情况下不起作用。
我有一页包含链接的图片。本质上我想删除图像周围的链接,但保持图像标签完好无损。
例如。我有:
<a href="something.html" alt="blah"><img src="image1.jpg" alt="image 1"></a>
我想要:
<img src="image1.jpg" alt="image 1">
我尝试了我在研究中发现的这段代码,但它留下了一个杂散的 </a>
标签。
$content =
preg_replace(
array('{<a(.*?)(wp-att|wp-content\/uploads)[^>]*><img}',
'{ wp-image-[0-9]*" ></a>}'),
array('<img','" />'),
$content
);
我对正则表达式一窍不通,请问有人可以修改我的代码吗? :-)
根据您提供的正则表达式,您似乎正在使用 wordpress 并希望从内容中删除超链接。
如果您使用的是 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;
}
这是另一个同样有效的函数。
function attachment_image_link_remove_filter($content)
{
$content =
preg_replace(array('{<a[^>]*><img}', '{/></a>}'), array('<img', '/>'), $content);
return $content;
}
add_filter('the_content', 'attachment_image_link_remove_filter');
或者您也可以使用 Demo
$string = '<a href="something.html" alt="blah"><img src="image1.jpg" alt="image 1"></a>';
$result = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\2", $string);
echo $result; // this will output "<img src="image1.jpg" alt="image 1">"
可以用<a.*?(<img.*?>)<\/a>
来匹配替换成</code></p>
<p>见<a href="https://regex101.com/r/xX9pJ8/1" rel="nofollow">DEMO</a></p>
<pre><code>$content = preg_replace('/<a.*?(<img.*?>)<\/a>/', '', $content);
我认为
$content = preg_replace('/<a\s+href=[^>]+>(<img[^>]+>)<\/a>/', '', $content);
是更好的解决方案。
例如:https://regex101.com/r/3ozruM/1
因为@karthik manchala 的解决方案https://regex101.com/r/ETkE58/1 在这种情况下不起作用。