Selasa, 17 Juni 2014

Getting an Image of The HTML Article with PHP preg_match Function

This case occurs in many blog.

You want to show a brief description of an article along with the image which is contain in it article.

You can see what I mean as shown below;

In that case, you don't need to create a new field contain an image in it. You just need to parse the img tag.
This is the code:

private static function getImageInPost($post_content)
{
    $jum = preg_match_all('/src="([^"]*)"/', $post_content,$matches);
        
    if($jum)
    {
        foreach ($matches as $match) 
        {
            if(strpos($match[0], "src")!== false)
            {
                $res = explode("\"", $match[0]);
                $image = "<img src='".$res[1]."'>";
            }
        }
    }
    else 
    {
        $image = "";
    }
        
    return $image;
}

Above code returning first image in the article.

1 komentar:

  1. Why casinos are rigged - Hertzaman - The Herald
    In the UK, ventureberg.com/ casino herzamanindir games are rigged and there is evidence of fraud, 출장마사지 crime or disorder or an individual's involvement. There are also worrione many 토토

    BalasHapus

Apa itu blockchain

Blockchain adalah sebuah database bersama yang datanya tidak dapat diubah. Blockchain  memfasilitasi proses pencatatan transaksi dan pelacak...