2

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

6

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>';
7
  • can we use explode() to achieve this @ialocin? Commented Sep 24, 2014 at 11:42
  • not sure I get what you mean @kiruthiga Commented Sep 24, 2014 at 12:08
  • okay @ialocin!!You are saying that you not sure about using explode() in this situation right @ialocin? Commented Sep 24, 2014 at 12:16
  • @kiruthiga to be honest, I just don't know what you are trying to do with the explode() Commented Sep 24, 2014 at 13:56
  • $img_img=explode('<img src="',$content); shall we use like this Commented Sep 25, 2014 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.