Trying to remove the image from the function previous_image_link () directly through the arguments of this function I do this is not possible. Therefore, try to simply extract the SRC:
<td style="background: url(
<?php
$html = previous_image_link('full');
preg_match_all('/<img [^>]*src=["|']([^"|']+)/i', $html, $matches);
foreach ($matches[1] as $key=>$value) {
echo $value;
}
?>
) no-repeat center center; background-size: cover;">
However, instead of the expected
<td style="background: url(/wp-content/uploads/2015/03/tiaurus.info_20150323125918.jpg") no-repeat center center; background-size: cover;">
getting
<td height="3166" class="attachment-full" alt="img_alt_text" src="/wp-content/blablabla/20150323125918.jpg" 8176"="">) no-repeat center center;background-size: cover;">
</td>
) no-repeat center center;background-size: cover;">
As in my case it is necessary to use preg_match_all?
Or can someone tell me how to get the SRC previous and next images for use in image.php?
Instead of trying to use a regex to extract the URL, I think you should try to reverse engineer the
previous_image_link
function to find a more direct approach.previous_image_link
is essentially a wrapper aroundadjacent_image_link
. Once you see how the adjacent image is being fetched, it is pretty easy to write a function that fetches the image src instead of the formatted link:You could also just return
$attachment_id
and then you could get whatever information you need about the attachment.You can use PHP function
pre_match
and eliminate theforeach
loop.Since your
$html
string only has onesrc
value to retrieve,preg_match
is sufficient.preg_match_all
is designed for multiple matches.Also, since
src=
is unique in the string, we can narrow down our pattern to something that starts withsrc=
, and is wrapped by either double quotes or single quotes.["']
is a character class that limits the match to one character that is either a double quote or a single quote. (The single quote is escaped because I’m using single quotes to delimit the regular expression string.)The parenthesis
()
capture a value and add it to the$matches
array.[^]
is a negative character class, in other words, don’t match any of the characters listed here.+
matches one or more of the previous character. So[^s]+
is simply saying, match one or more characters that are not a space character.Given:
echo $matches[1];
returns: