Log in

View Full Version : Questin about regular expression



die-trying
12-02-2012, 10:24 AM
hi guys how are you ?

i have question regarding regular expression

let me get to the point

for example


<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="robots" content="index, follow" />
<meta name="keywords" content="joomla, Joomla" />
<meta name="description" content="Joomla! - the dynamic portal engine and content management system" />
<meta name="generator" content="Joomla! 1.5 - Open Source Content Management" />

i built one script using file get contents .. i used preg match to search for specific words from the above source

suppose i just want to search for joomla version


how i could do that please ..



thank you

traq
12-02-2012, 08:27 PM
Are you searching the entire document as *text* (not parsing it, using DOMDocument (http://php.net/domdocument) or similar)?

If so, this "works," but relies on the <meta> tag being generated in a predictable manner
(i.e., the first part must always be identical, and the version number must always immediately follow):
<?php

# `$htmlstring` must be the html document you're searching

if( preg_match( '#<meta name="generator" content="Joomla\! ([0-9]\.[0-9])#ui',$htmlstring,$match ) ){
$joomla_version = $match[1];
print $joomla_version;
}

Using DOMDocument would be a more reliable way of getting the content attribute, and would allow a simpler regex. If you're trying to get more than this one value from the document, there might also be a significant performance improvement.