Hello,
I am trying to get the first image of the text field "blog_post_content" and copy it to the field "blog_post_image".
I was looking on the Internet and tried something but I have an error.
In the controller I have this function:
PHP Code:
public function getfirstimage() {
$sql = "SELECT blog_post_id, blog_post_content FROM tbl_blog_post";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$id = $row->blog_post_id;
$texthtml = $row->blog_post_content;
$first_img = '';
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $texthtml, $matches);
$first_img = $matches[1][0];
$data = array('blog_post_image' => $first_img);
$this->db->where('blog_post_id', $id);
$this->db->update('tbl_blog_post', $data);
}
}
}
The error is:
PHP Code:
Message: Undefined offset: 0 ... in line "$first_img = $matches[1][0];"
Can somebody help me with it?
Thank you.