I copied your code to a file in my home directory on my Ubuntu box, removed the weird character that @s_ha_dum noticed, and ran it. It worked as I’d expect.
Code:
<?php
function get_the_post_imgs($oldPostContent) {
$newPostContent = $oldPostContent;
$newPostContent = preg_replace_callback(
"/<img.*src=[\"']([^\"']*)[\"'].*>/i", // pattern to match to (i.e the contents of an <img src="https://wordpress.stackexchange.com/questions/140017/..." /> tag)
function ($match) {
return "hello world"; // obviously it's a lot more complicated than this but for your sake i've massively simplified it so the src="https://wordpress.stackexchange.com/questions/140017/..." content is just replaced with "hello world" for now
},
$oldPostContent
);
return $newPostContent; // return new manipulated content
}
echo get_the_post_imgs( '<a href="https://wordpress.stackexchange.com/questions/140017/foo.php"><img align="right" src="http://example.com/fire.gif" /></a><br />' );
?>
Running this code from the command line:
% php -e foo.php
<a href="https://wordpress.stackexchange.com/questions/140017/foo.php">hello world
Your regex might be a little greedy for your purposes, but it works.
PS, trying to run your code with the weird character still in place:
% php -e foo.php
PHP Parse error: syntax error, unexpected '"/<img.*src=[\"']([^\"']*)[\"'' (T_CONSTANT_ENCAPSED_STRING) in /home/username/foo.php on line 6
Edit — Since you asked in the comments, I thought I’d try to cobble up a less greedy regex for you. Try this on for size:
<img.*src=[\"']([^\"']*)[\"'][^>]*>
The extra [^>*]
term at the end ends the <img...>
tag at its closing >
, instead of carrying forward to the last >
in the line (in my example, the closing >
in the </a>
tag).