here is my code with problems:

$url = 'somesite/somepage';

$str = file_get_contents($url);

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

print_r($matches[0]);

the goal is to type in any webpage and get its source code into
$str and then find the first appearance of the symbol $ in the string
and return only the number closest to that symbol

1.is there something like screenscrape that not gives me the pictures and layout , just the source code so I can put it in a string like text ?

2.when I replace the "$str = file ..." with
"$str = 'hgfghfgh 7656575 ghfghf 160 $ hfgcgh 340 $ gffg$'"
I get all the text before the $ but I want only the nomber before the first $ returned and thats it. how should the preg_match be written?

print_r($matches);

The $matches[1] should contain what you want. The sub matches ie: anything between ( and ) go into their own indexes in $matches starting from 1 being the first (). $matches[0] contains the whole match.

This would be a lot faster with single string functions.

eg:

strpos() to get the first occurance of $.
Then get each character backwards from the $ with a loop like: while().
end when you have a string of digits, and encounter a non-digit.
strrev() to reverse the digits.

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

this option is good for the case where $ comes after the number but I see that I actually need the option that the number is after the $ like so: $ 199

$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

$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

Would this work?

$regex = "/(\d+)\s\\$|\\$\s(\d+)/";

I will match any string of digits before a space before $.
and any string after a $ and a space.

Also use preg_match_all if you want to get all matches.

I'd recommend something like this though:

function get_digits($string) {
	$len = strlen($str);
	$digits = array();
	$offset = 0;
	while(($i = strpos($str, '$', $offset)) !== false) {
		if ($str[$i] == '$') {
			
			// check before $ for digits
			$j = $i-2; // skip the space before $
			$digit = '';
			// while we have numric values, count backwards
			if (isset($str[$j])) {
				while(is_numeric($str[$j])) {
					$digit = $str[$j].$digit;
					$j--;
				}
				if ($digit) {
					$digits[] = $digit;
				}
			}
			
			// check after $ for digits
			$j = $i+2; // skip the space before $
			$digit = '';
			// while we have numric values, count backwards
			if (isset($str[$j])) {
				while(is_numeric($str[$j])) {
					$digit .= $str[$j];
					$j++;
				}
				if ($digit) {
					$digits[] = $digit;
				}
			}
		}
		$offset = $i+1;
	}
	return $digits;
}

Though a lot more code, it is 50% faster.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.