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

Ken Snyder kendsnyder at gmail.com
Fri May 16 09:39:00 CDT 2008


Stefan Schwarzer wrote:
> Hi there,
>
> I am sure there is an efficient way to extract some part of a string.  
> I have a string which looks like this:
>
> 	...<br />% countries: <b>73%</b><br />% population: <b>98%</b><br />%  
> economy (GDP): ....
>
> Now, I would like to extract the value which is to be found behind the  
> "population: <b>".... As the string can vary in length I can't use a  
> substring_number-from-left... thing. I could split the string behind  
> "population: ", than eliminate "<b>" etc... But that seems rather  
> complicated and ugly.
>
> Can anyone give be a hint how I can achieve that nicely?
>
> Thanks for any advice.
>
> Stef
>   
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.

- Ken Snyder




More information about the thelist mailing list