Click to See Complete Forum and Search --> : Retrieve decimal numbers from a string


yunfanny
07-28-2006, 11:44 PM
Hi,

Let say I have a file containing strings like :
1. News Flash: Small Businesses Create Jobs (Similarity = 0.168575)
2. Small Busines Trends Radio: Top 10 Online Marketing Mistakes and How to Avoid Them (Similarity = 0.156381)
3. Podcast Advertising...Your Path to the Small Business Market? (Similarity = 0.109887)
......

I want to get all the decimal numbers from the string and output as:
0.168575
0.156381
0.109887
......

How can I do this? :confused:

Thank you :)

pcthug
07-29-2006, 12:45 AM
Try the following:

<?php
// where $str is your file read into a string
preg_match_all("'\(Similarity = ([0-9]\.[0-9]{6})\)'", $str, $array, PREG_PATTERN_ORDER);

function output_matches($key)
{
echo "$key\n";
}

// output matches to the browser
array_walk($array[1], 'output_matches');
?>
It matches all instances of (Similarity = *.******), where * is a numeric character.

yunfanny
07-29-2006, 01:39 AM
Hi, thanks, pcthug. It is working !

pcthug
07-30-2006, 11:34 PM
Please note this script will only match 7 digit decimal numbers.