get image url inside the content in wordpress?

I want to get the image_url from the content.Actually i have done the following code to get the content:

$content=$query_val->post_content;

i’m getting following as o/p:


“content”:”Lorem Ipsum-has been the industry’s standard dummy text,Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s…”


When I add the strip_tags() like :

 $content=strip_tags($query_val->post_content);

It will remove all the tags, so that i could not get the image_url which is inside the tag.

Now I want to get the image url inside the content like:


“content”:”Lorem Ipsum-has been the industry’s standard dummy text,Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s,Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s…image_url:”imageurl”


Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Sounds like you need to do some regular expression on your post content. Which could be done for example like this:

// get the post object
$post = get_post( get_the_ID() );
// we need just the content
$content = $post->post_content;
// we need a expression to match things
$regex = '/src="([^"]*)"/';
// we want all matches
preg_match_all( $regex, $content, $matches );
// reversing the matches array
$matches = array_reverse($matches);
echo '<pre>';
// we've reversed the array, so index 0 returns the result
print_r($matches[0]);
echo '</pre>';


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x