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”


1 Answer
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="https://wordpress.stackexchange.com/questions/162402/([^"]*)"https://wordpress.stackexchange.com/";
// 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>';

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *