Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

Get meta tags from file (description ank keywords)

Hi E's, I want get meta tags description and keywords of a file, but I don't want use "get_meta_tags" function.
I need help for create a regular expression for meta tags descriptio and keywords.

Regards, JC
Avatar of abel
abel
Flag of Netherlands image

Along the lines of your other question, this could be (result in $1$2):

/<meta[^>]+http-equiv="description" content="([^"]+)"|'([^']+)'/
/<meta[^>]+http-equiv="keywords" content="([^"]+)"|'([^']+)'/
The regex is a bit minimalistic, here's a more sophisticated try:

/<meta[^>]+http-equiv="description"[^>]+content=(?:"([^"]+)"|'([^']+)')/msi
/<meta[^>]+content=(?:"([^"]+)"|'([^']+)')[^>]+http-equiv="description"/msi
/<meta[^>]+http-equiv="keywords"[^>]+content=(?:"([^"]+)"|'([^']+)')/msi
/<meta[^>]+content=(?:"([^"]+)"|'([^']+)')[^>]+http-equiv="kewyords"/msi

You can put them in an array for applying repeated regexes, if you use PHP. The first two are for the order of the attributes. Most people code the first version, but the second version is also allowed. The regex must have the /smi modifier, because the statements may span multiple lines
Avatar of Pedro Chagas

ASKER

Hi @abel, I try to use your regular expression in this way, but I get this error:
Parse error: syntax error, unexpected T_STRING in /home/tags/public_html/tp_relatorio.php on line 187

My code is:
preg_match("/<meta[^>]+http-equiv="description" content="([^"]+)"|'([^']+)'/"), $txt, $des);

What is wrong?

Regards, JC
Same as with the other reqular expression. I gave you the whole regex, how you use it depends on how to escape it. In your way, you still have to escape the quotes (or use single quotes, but that won't help you here).

preg_match("/<meta[^>]+http-equiv=\"description\" content=\"([^\"]+)\"|'([^']+)'/"), $txt, $des);

let me know if there are any other errors.
Hi , with:
preg_match("/<meta[^>]+http-equiv=\"description\" content=\"([^\"]+)\"|'([^']+)'/"), $txt, $des);

I get this error:
Parse error: syntax error, unexpected ',' in /home/tags/public_html/tp_relatorio.php on line 190

Where is the error?

Regards, JC
You have an extra closing parenthesis, not sure why, it appeared in your earlier comment too, but I didn't notice it then. This should help:

preg_match("/<meta[^>]+http-equiv=\"description\" content=\"([^\"]+)\"|'([^']+)'/", $txt, $des);

use $des[1] and $des[2] for the finds.
Hi @abel, I don't know what happening, but your regular expression don't work.
I do some tests with deferent sites, inclusive I call the variable with print_r to see all container and I get this examples:
================================
Array ( [0] => ') {return false;}">  
other example
Array ( [0] => 'http://ipt.olhares.com/data/th/279/2792787.jpg' [1] => [2] => http://ipt.olhares.com/data/th/279/2792787.jpg )
other example
Array ( [0] => '-//W3C//DTD HTML 4.0 Transitional//EN' [1] => [2] => -//W3C//DTD HTML 4.0 Transitional//EN )
======================================

What is wrong?

Regards, JC
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad it works. :)