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.

  1. do have modify pattern , replacement?
  2. 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

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -