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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With