<?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
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$? Ie 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] => $
$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
if you get array printed > try echo $matches[0]; or echo $matches[1];
// update
// test2.php lkadf $ lksajdf;
// test.php
$url = 'test2.php';
$raw = file_get_contents($url);
preg_match('/\w(\d*).*?\$/',$raw,$matches);
echo $matches[0]; ?>
Will output:
lkadf $ (the first part).
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.