tags:

views:

146

answers:

7
<?php
$url = 'here i give a url';

$raw = file_get_contents($url);

preg_match('/\w(\d*).*?\$/',$raw,$matches);


echo $matches;
?>

in a given external website I need the closest number to the first appearance of a symbol here the symbol is $

this is what I tried for now I just get "array" printed

A: 
Maurice Kroon
A: 

I changed to $matches[0]; or echo $matches[1]; now I get nothing...

Please don't post comments to your question as answers
soulmerge
A: 

Use preg match with $matches[1], but you need to change your regex to

preg_match('/\w(\d+).*?\$/',$raw,$matches);

The * matches zero or more times, when really you want at least one digit. Change it to + so you match one or more.

Ian Elliott
A: 

I still do not get anything. I put both files in the public_html dir

Try to respond to answers with comments, and what public_html dir?
Ian Elliott
"array" will always be returned, even if it's empty. It looks like the regexp is not matching anything in the text you're giving it. Can you give us an example of the input / symbol you're looking for?
GaZ
+2  A: 

Am I interpreting your question correctly if you, given the following input:

<div>
   <span>12.92$</span>
   <span>24.82$</span>
</div>

want to get back 12.92$? I.e. you want the first occurrence of an amount of money from a web site?

If so, try the following:

// Will find any number on the form xyz.abc.
// Will NOT cope with formatting such as 12 523.25 or 12,523.25
$regex = '/(\d+(:?.\d+)?) *([$])/'
preg_match($regex, $website, $matches);

//matches[0] => 123.45$
//matches[1] => 123.45
//matches[2] => $

Another, more ambitious try (Which could fail more often) would be:

// Will find any number on the form xyz.abc.
// Attempts to cope with formatting such as 12 523.25 or 12,523.25
$regex = '/(\d{1,3}(:?[, ]?\d{3})*(:?.\d+)?) *([$])/'
preg_match($regex, $website, $matches);

//matches[0] => 12 233.45$
//matches[1] => 12 233.45
//matches[2] => $
PatrikAkerstrand
A: 

ok It works when I use the second php file as you said but that is not relevant cause when I write a page address it gives me nothing even if I use print_r ()

A: 

$regex = '/([$])* (\d+(:?.\d+)?) /' ; preg_match($regex, $str, $matches);

it works I just switched the sides for $199 !

now I need also an option to get only the second apearance:

code ..... $199 ......

.... $299 ....

matches[2] should print 299