drupal input filter changes only one pattern

168 views Asked by At

my custom drupal module provides a custom input filter, the function is below:

function my_custom_filter($text) {

return preg_replace('~<img(.*)src=\"/sites/default/files/(.*)\"~', '<img$1src="' . variable_get('static_url', "http://fileserver.com") ."/". file_directory_path() . "/" . '$2' . "\"", $text);

}

as you can see, i use the module for an cdn fileserver change for the images entered in rte (i use tinymce).

The problem is, my filter only changes the last pattern of the given text. I don't understand why this happens, any ideas?

1

There are 1 answers

0
John Fiala On

I think the problem is that your .* is being too greedy, and selecting most of the text - from the first '<img' to the last 'src='

Try adding the pcre pattern modifier U (that's capital U) after the second pattern ~. That will invert the .* parts of the pattern to become ungreedy, and to match as few characters as possible.