How To Grab Meta Tags Off Of A Page?

Fellow Php Folks!

Have you ever tried grabbing the meta tags and page title of the page you are currently at, using php in procedural style ?

Here’s an OOP style not working:


<?php 

$url='http://stackoverflow.com/questions/3711357/get-title-and-meta-tags-of-external-site/4640613';
function get_meta_tags ($url){
         $html = load_content ($url,false,"");
         print_r ($html);
         preg_match_all ("/<title>(.*)<\/title>/", $html["content"], $title);
         preg_match_all ("/<meta name=\"description\" content=\"(.*)\"\/>/i", $html["content"], $description);
         preg_match_all ("/<meta name=\"keywords\" content=\"(.*)\"\/>/i", $html["content"], $keywords);
         $res["content"] = @array("title" => $title[1][0], "descritpion" => $description[1][0], "keywords" =>  $keywords[1][0]);
         $res["msg"] = $html["msg"];
         return $res;
}
?>

Can anyone fix this to procedural style and get it to work ?
The simpler, for a beginner, the better!

Another attempt but no luck again:
http://php.net/manual/en/function.get-meta-tags.php


<?php
// Assuming the above tags are at www.example.com
$tags = get_meta_tags('http://www.example.com/');

// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.
echo $tags['author'];       // name
echo $tags['keywords'];     // php documentation
echo $tags['description'];  // a php manual
echo $tags['geo_position']; // 49.33;-86.59
?>

No luck agaion for the 3rd time:



<?php $tags = get_meta_tags('https://stackoverflow.com/questions/14950548/how-to-extract-meta-tags-from-website'); 

echo $tags['description']; ?><br /><br />
<?php echo $tags['keywords']; ?>

What makes you think that that first bit of code is ‘OOP-style’ instead of ‘Procedural-style’?

1 Like

Because PDO is OOP style and Mysqli_ is Procedural.
PDO uses one or both of these:

=>

That is one way I id it. Unless ofcourse, you have a better id tag. :slight_smile:

NOTE: I know you guys really look at the structure to spot the style but I don’t have an eye to spot this as of now as I don’t know much difference between them in terms of look.
I know in OOP you reference a lot to blocks of code that can be used & re-used over and over again while in procedural style you can’t do that. Like in html we reference to header & footer files rather than add the same ad codes on each and every webpage. You know what I mean.

Although “->” and “=>” look somewhat similar, “=>” in this case is assigning a value to an array key.

Also, while developing, please remove that “@” error suppressor from the @array(

1 Like

Please note that when developing locally it is best to declare strict_types, error_reporting and ini_set display errors.

I am amazed after the numerous replies to your other posts explaining in great detail exactly how to use the above features… all have been completely omitted?

2 Likes

Frankly, I did remember about them but just did not bother as I saw without them I was getting errors. So, thought to myself, if the errors are given without them then why bother them ? Maybe, the new NotePad++ has them built-in ? Or, how else does it explain the errors I was getting without these error reporting codes ?

What is the benefit of removing it ? And what harm does it do if it stays ? Have to learn this answer. He! He!
I just copied the whole code from:

What is the benefit of removing what?

You need to learn to ask clear questions if you wish to receive clear answers.

Would have been best if this forum showed quotes (the sentence/paragraph) of the person to whom I am replying to. Like other forums.

Anyway, Mittineague asked:

Also, while developing, please remove that “@” error suppressor from the @array(

And so, I made the questions to him.

It does indicate the person to whom you’re replying. In this case, as you can see from the icon in the top righthand corner, you are replying to yourself. (And of course it will show quotes, if you choose to do so.)

I’m not sure why you are unable to look up even the simplest thing for yourself, but anyway:

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.

http://php.net/manual/en/language.operators.errorcontrol.php

1 Like

Yes Techno Bear, it shows Icon to whom I am replying to but I don’t see it adding that person’s mssg on auto.
For example, I am replying to you this post. Your icon is shown as the person to whom I am replying to. But where is your post quoted so others can see to what mssg of you I am replying to ? They’d have to scroll up the page and read your post to figure-out to what I am replying to. That’s how I figure things out here. And all this scrolling up and down is getting tiring now.
On other forums, as soon as you click the REPLY, the person to whom you are replying to, his mssg shows-up as quote on auto without us having to do any extra step.

As for, why I don’t do a simple thing and check the php.net manual. Because to me it is useless. Like trash. Bible for geeks like you but for beginners like me it puts me off. I checked your link out now:
http://php.net/manual/en/language.operators.errorcontrol.php
And guess what, it gave me a headache. Didn’t quite understand what it was going on about. Had a slight hunch that it was saying that, if I don’t want the error to be shown on a specific part of the code (because the error reporting code is in place and would get triggered) then to add the “@” and the error reprting code would ignore that part of the code and not show the error. But like I said, all this is just guess-work. But reading your reply has confirmed my guess. So, you see, I understand people here than that wretched manual. Which keeps putting me off from php. The only thing that keeps me going is the tutorials like the codeacademy.com, tutorialspoint.com and tutorialrepublic.com. I do check the outdated tizag.com as a reference over the php.net manual. And most of all, what motivates me to not dump php in favour of python (a simpler language) is this forum and it’s volunteers. Otherwise, I would have dumped php 2 months back. Been Php-ing for 6 mnths now, 7 days a week. I only probably took 2 days off when I was ill and a day off due to boreness out of these 6 months. And so, you can understand why I easily get tired reading the likes of the manual. Does my head-in.
Don’t bother replying, if you want to give a bollocking. But, if you want to give any advice on how to learn faster, how not to get bored and how to get more motivated, etc. then by all means be my guest. :wink:
I’m gonna listen to music on the background now and take a little break. I stick to my php projects the whole night (midnight till 6/7am). Been doing it for 6 mnths now. My projects are nearly finished. And the thought of finishing it tonight looms in my head everynight and been doing so for 4 months now and that keeps me going. Else, the messy syntax of php would have made me dump it 4 mnths back.

Have you tried clicking on the top of the quoted text at all?

Also, have you ever though of putting a line break between your paragraphs to help other in reading your wall of text posts?

1 Like

Ah! I found the icon now. I was trying the " and it was not working.

Cheers!

I’m so glad you found my reply clear and easy to understand. It is, as I’m sure you realised, a direct quote from the manual, which I also linked to for reference.

1 Like

That’s not quite true, mysqli can be used in OOP style as well.

3 Likes

Yeah. I figured you quoted from the manual. But guess what ? Read that page of the manual atleast one screen size of the page and then read only the part you quoted and then tell me whether your quoted part seems easier to understand or whether the manual’d full explanation (which ofcourse includes your quote). You see, if you read too much, you forget what you read at the top and get confused. Info overload.
No need to reply.

Good, then.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.