Regex pattern to match all images with replacement holding self PHP function -
i have preg_replace
function find images , wrap them inside <figure>
tag different class, depends on image source:
$pattern = '"/<img[^>]*src="([^"]+)"[^>]*alt="([^"]+)"[^>]*>\i"e'; $replacement = '"<figure class=\"" . check($1) . "\"><img src=\"$1\" alt=\"$2\" /></figure>"'; preg_replace($pattern, $replacement, $content);
therefore, put right class, wish call function check($source)
image source parameter. way, function going return necessary class. as can see in code above, trying use e modifier, seems doesn't work.
- do have modify pattern , replacement?
- should use
preg_replace_all
find images, if many inside$content
variable?
you can use preg_replace_callback()
purpose. allows define , use function replacement. function should expect array of matches , supposed return replacement value.
preg_replace()
e
modifier trick.
Comments
Post a Comment