homepage Welcome to WebmasterWorld Guest from 23.22.29.137
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
regex: get value
camilord




msg:4658121
 3:03 am on Mar 28, 2014 (gmt 0)

hi.. need help again with regex...

hope readie will be posting his answer here.. hehee..

this is the string...

PRETTY_NAME="Kali GNU/Linux 1.0"
NAME="Kali GNU/Linux"
ID=kali
VERSION="1.0"
VERSION_ID="1.0"
ID_LIKE=debian
ANSI_COLOR="1;31"
HOME_URL="http://www.kali.org/"
SUPPORT_URL="http://forums.kali.org/"
BUG_REPORT_URL="http://bugs.kali.org/"


i need to extract the value of NAME or PRETTY_NAME...

what would be the pattern?

 

camilord




msg:4658198
 9:57 am on Mar 28, 2014 (gmt 0)

got the solution already... :)

preg_match_all('/(\w+)\s*=\s*(["\'])((?:(?!\2).)*)\2/', $this->_server_data['lsb'], $result, PREG_SET_ORDER);
if (isset($result[0][3])) {
return $result[0][3];
}

Readie




msg:4658245
 12:56 pm on Mar 28, 2014 (gmt 0)

Would have posted an answer :P

camilord




msg:4658314
 7:02 pm on Mar 28, 2014 (gmt 0)

hhehehe... anyway.. thanks Readie...

penders




msg:4658340
 10:59 pm on Mar 28, 2014 (gmt 0)

If you just need to match the value of PRETTY_NAME (or any single field), can this not be simplified to something like:

preg_match('/PRETTY_NAME="(.+?)"/', $this->_server_data['lsb'], $result);
return isset($result[1]) ? $result[1] : false;


?

robzilla




msg:4658354
 1:05 am on Mar 29, 2014 (gmt 0)

If PRETTY_NAME is always present, and the value is all you need, you could avoid regular expressions like so:

$prettyname = trim(str_replace('"','',str_replace('PRETTY_NAME="','',substr_replace($string,'',strpos($string,"\n")))));

Where strpos finds the position of the first linebreak, substr_replace then removes everything that comes after that position, str_replace then removes PRETTY_NAME=" at the front of the remaining string and the closing " at the end. If PRETTY_NAME is not always present, you could add another str_replace for NAME=":

$prettyname = trim(str_replace('"','',str_replace('NAME="','',str_replace('PRETTY_NAME="','',substr_replace($string,'',strpos($string,"\n"))))));

Though if you prefer to use preg_match, the simplest expression I could come up with is this:

preg_match('#(PRETTY_)?NAME="(.+)"#',$string,$match);

Where the value of PRETTY_NAME or NAME (whichever comes first) would then be stored in $match[2].

However, it's best to avoid regular expressions when you can. I ran both methods 10 million times, and the first method was at least 4x faster than preg_match.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved