Pedro Chagas
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
I need help for create a regular expression for meta tags descriptio and keywords.
Regards, JC
The regex is a bit minimalistic, here's a more sophisticated try:
/<meta[^>]+http-equiv="des cription"[ ^>]+conten t=(?:"([^" ]+)"|'([^' ]+)')/msi
/<meta[^>]+content=(?:"([^ "]+)"|'([^ ']+)')[^>] +http-equi v="descrip tion"/msi
/<meta[^>]+http-equiv="key words"[^>] +content=( ?:"([^"]+) "|'([^']+) ')/msi
/<meta[^>]+content=(?:"([^ "]+)"|'([^ ']+)')[^>] +http-equi v="kewyord s"/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
/<meta[^>]+http-equiv="des
/<meta[^>]+content=(?:"([^
/<meta[^>]+http-equiv="key
/<meta[^>]+content=(?:"([^
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
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[^>]+htt p-equiv="d escription " content="([^"]+)"|'([^']+) '/"), $txt, $des);
What is wrong?
Regards, JC
Parse error: syntax error, unexpected T_STRING in /home/tags/public_html/tp_
My code is:
preg_match("/<meta[^>]+htt
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[^>]+htt p-equiv=\" descriptio n\" content=\"([^\"]+)\"|'([^' ]+)'/"), $txt, $des);
let me know if there are any other errors.
preg_match("/<meta[^>]+htt
let me know if there are any other errors.
ASKER
Hi , with:
preg_match("/<meta[^>]+htt p-equiv=\" descriptio n\" 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
preg_match("/<meta[^>]+htt
I get this error:
Parse error: syntax error, unexpected ',' in /home/tags/public_html/tp_
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[^>]+htt p-equiv=\" descriptio n\" content=\"([^\"]+)\"|'([^' ]+)'/", $txt, $des);
use $des[1] and $des[2] for the finds.
preg_match("/<meta[^>]+htt
use $des[1] and $des[2] for the finds.
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Glad it works. :)
/<meta[^>]+http-equiv="des
/<meta[^>]+http-equiv="key