[thelist] Extract a specific but changing part of a string (PHP)

Stefan Schwarzer st.schwarzer at geois.de
Mon May 19 13:53:34 CDT 2008

> You'll want to dive into regular expressions.  For example in PHP with
> perl-compatible regular expressions:
> preg_match('/population\: <b>([0-9]+)<\/b>/', '<br />population:
> <b>98</b><br />', $match);
> echo $match[1]; // 98
> That regular expression (the first argument) looks for the string
> "population: <b>" followed by one or more numbers followed by "</b>".
> You can also do one expression that will extract multiple values:
> preg_match(
>  '/countries\: <b>([0-9]+)<\/b><br \/>population\: <b>([0-9]+)<\/b><br
> \/>economy \(GDP\)\: <b>([0-9]+)<\/b>/',
>  'countries: <b>73</b><br />population: <b>98</b><br />economy (GDP):
> <b>44</b>',
>  $matches);
> echo $match[1]; // 73
> echo $match[2]; // 98
> echo $match[3]; // 44
> There are similar functions for extracting values from a string with
> regular expressions in almost all other languages.

Thanks a lot Ken,

I never had the courage to dive into that domaine... regular  
expressions... looked too complicated to me, although I knew that  
there tons of possibilities. So thank you to make me push and learn  
swimming now...


More information about the thelist mailing list