PHP Regex to get img inside WP Caption -
i want img tag inside caption tag
example:
[caption id="attachment_5433" align="aligncenter" width="413"] <a href="abc.jpg"><img class=" wp-image-5433" title="this title" src="abc.jpg" alt="this alt" width="413" height="551"></a>this desc [/caption]
and result:
<img class=" wp-image-5433" title="this title" src="abc.jpg" alt="this alt" width="413" height="551">
how php regex?
<?php $string = '[caption id="attachment_5433" align="aligncenter" width="413"] <a href="abc.jpg"><img class=" wp-image-5433" title="this title" src="abc.jpg" alt="this alt" width="413" height="551"></a>this desc [/caption]'; $result = preg_match_all("/\[caption.*?].*?(<img.*?\/?>).*?\[\/caption]/s", $string, $matches); print_r($matches); ?>
ouput
array ( [0] : array ( [0] : '[caption id="attachment_5433" align="aligncenter" width="413"] <a href="abc.jpg"><img class=" wp-image-5433" title="this title" src="abc.jpg" alt="this alt" width="413" height="551"></a>this desc [/caption]' ) [1] : array ( [0] : '<img class=" wp-image-5433" title="this title" src="abc.jpg" alt="this alt" width="413" height="551">' ) )
update, replace callback
<?php $string = '[caption id="attachment_5433" align="aligncenter" width="413"] <a href="abc.jpg"><img class=" wp-image-5433" title="this title" src="abc.jpg" alt="this alt" width="413" height="551"></a>this desc [/caption]'; echo preg_replace_callback('/\[caption.*?].*?(<img.*?\/?>).*?\[\/caption]/s', function($matches) { return $matches[1]; }, $string); ?>
Comments
Post a Comment