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.

Tidak ada komentar:

Posting Komentar

Cara Mengetahui Besar Database PostgreSQL Tanpa Mendownloadnya

Berikut adalah langkah-langkah untuk mengetahui ukuran semua database di instance PostgreSQL yang berjalan di dalam kontainer Docker: 1. Men...