This is what the function returns:
http://www.domain.com/wp-content/uploads/2014/09/image.jpg
This is what I need:
wp-content/uploads/2014/09/image.jpg
function catch_first_image()
{
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches );
$first_img = $matches[1][0];
echo $first_img;
}ตอบ
$output = preg_match_all( "/<img.*https?:\/\/[^\/]+\/([^\"']+)[\"'].*/i", $post->post_content, $matches ); echo $matches[1][0];
Demo
https://ideone.com/8kKyDR
Explanation:
.* => any character zero or more times
https? => http or https
[^\/]+ => any character other than / at least one time
[\"'] => double or single quoteตอบ
Use this expression:
~<img.+?src=['"]http://www\.domain\.com/([^'"]+)['"].*?>~ihttps://regex101.com/r/kS6fK0/1
I just literally matched http://www.domain.com outside of your first capture group you reference. Note that I changed delimiters to ~ so we don’t need to escape slashes. You may want to change this to https?://(?:www\.)? to make things more flexible. Don’t forget to escape periods. Also, I made your dot-match-all repetitions lazy to save you from headaches in the future (.+? and .*?).
ตอบ
<img.+?https?://[^\/]+\/\K[^\"']+https://stackoverflow.com/questions/25876431/using-preg-match-all-to-return-part-of-images-url-in-wordpress