php - Regex to remove links surrounding images -


i have page of images wrapped links. want remove links surrounding images, keep image tags in tact.

eg. have:

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

and want:

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

i tried code found in research, leaves stray </a> tag.

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

i have no idea when comes regular expressions can please fix code? :-)

by provided regex seems using wordpress , want remove hyperlinks content.

if using wordpress can use hook remove hyper links on images content.

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; } 

here function work.

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'); 

or can use 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; // output "<img src="image1.jpg" alt="image 1">" 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -