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
LVL 4
Pedro ChagasWebmasterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

/<meta[^>]+http-equiv="description" content="([^"]+)"|'([^']+)'/
/<meta[^>]+http-equiv="keywords" content="([^"]+)"|'([^']+)'/
0
abelCommented:
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
0
Pedro ChagasWebmasterAuthor Commented:
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
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

abelCommented:
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.
0
Pedro ChagasWebmasterAuthor Commented:
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
0
abelCommented:
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.
0
Pedro ChagasWebmasterAuthor Commented:
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
0
abelCommented:
We should've started with proper input. Checking some sites shows that we should match this pattern:

<meta name="description" content="find this string" />
<meta name="keywords" content="find this string" />

Trying the regex from the second comment of me, replacing "http-equiv" with "name", gives the correct results. Of course, you still need proper escaping of the string if you delimit with single or double quotes.

I tried it online here: http://www.spaweditor.com/scripts/regex/index.php with the following unescaped regex: /<meta[^>]+name="description"[^>]+content=(?:"([^"]+)"|'([^']+)')/msi giving me the results as below for this very page.

-- Abel --

<!-- resultset of preg_match (need [1] and [2]) -->
Array
(
    [0] => <meta name="description" content="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..."
    [1] => 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...
)
 
 
<!-- source of this page that I used -->
<title>Get meta tags from file (description ank keywords) : php, regular expression, tags, meta tags, description, keywords</title>
<meta name="description" content="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..." />
<meta name="keywords" content="php, regular expression, tags, meta tags, description, keywords, Regular Expressions, PHP Scripting Language" />
<script src="http://images.experts-exchange.com/scripts/s_code_14738493f31e9bd92f18a9e16b6c4097.js" type="text/javascript"></script> </head>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abelCommented:
Glad it works. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.