Similar to Facebook, I am building an app that allows users to post a link.
The user fills out the link in an input field, and the controller returns
Title
Link
Meta description
Images (as thumbnails)
to the view.
Here is the controller code:
$url = $this->input->post('posts_link');
if (!empty($url)) {
$html = file_get_html($url);
foreach ($html->find('img') as $element) {
$src = "";
$src = $element->src;
if (preg_match("/\.jp[e]?g$/i", $src)) {
$images[] = $src;
}
}
$data['posts_link'] = $url;
$data['images'] = $images;
$data['title'] = $html->find('title', 0)->plaintext;
$data['meta'] = get_meta_tags($url);
The problem I'm having is when there are no images, no title, or no description (alone or in combination).
I am using codeigniter and it throws several errors on the view, which I would rather have suppressed.
Is there a best practice to suppress these errors or place empty variables in case no title/images/descriptions are returned by the DOM parser?
For example I've tried
$data['images'] = $images ? $images : '';
but it doesn't resolve my problem.
Any suggestions?
Thanks.
You need to predefine the variables before you use them. For $images
it would be
$images = array();
right after if (!empty($url)) {
etc
if (array_key_exists('images', $data)) {
// display the image
}
else
{
//set a default image, etc
}
or something like isset
you can suppress errors by using the @ symbol. i.e.
@ $data['images'] = $images;
精彩评论