$url = 'the web address I want to get the first and second numbers close to $' ;

$str = file_get_contents($url);



preg_match_all('/ ([$]) *(\d+(:?.\d+)?)/', $str, $matches, PREG_SET_ORDER);

$i=0;
foreach ($matches as $val) {
    
	if($i==0) $first=$val[2] ;
	if($i==3) $second=$val[2] ;

	
$i++;

}
$bad_symbols = array(",", "."); $first = str_replace($bad_symbols, "", $first); 
$bad_symbols = array(",", "."); $second = str_replace($bad_symbols, "", $second); 

echo $first . "</br>";
echo $second;

it worked fine till yesterday what could be the problem?

People tinker with the layout and format of web pages all the time.
Start by verifying the input data is as you expect (visit it with a browser).

It's possible that some server directive has been altered (or your site has been rehosted), and file_get_contents($url); is no longer retreiving file contents, particularly if the file is served from a different domain.

If so then you need to speak with your hosting company/server admins.

Airshow

I talked to my hosting service and they said nothing changed

I have a second version that loops many requests and it gives this error which didn't before

[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

Avukado,

As Salem says - debug statement(s).

Echo $url, $str and $matches and see if they are set to what you expect.

Airshow

ok I think I found the problem
during debug as I printed the $str
the site has been changed
now there is a word between the price and the symbol
like this:
798 :price $

how do I change the code:

preg_match_all('/([$])*(\d+(:?.\d+)?)/', $str, $matches, PREG_SET_ORDER);

I mean the echo $str gives me '798 :price $'
but when I manualy read the source code it is
' price: <span>
798 $</span> '

Mmm, that's a bit odd because the words in the string are in a different order.

Assuming:

'    price: <span>
                798 $</span>  '

to be what is actually served, then try:

$strArray = explode(' ', strip_tags(trim($str)));
$price = trim($strArray[1]);
$currency = trim($strArray[2]);

That should give $price as '789' and $curency as '$'. I can't work out exactly what you want in your $first and $second so you will probably need to play around with it a bit.

Airshow

what is it"not doing"?
Did you check the error log? What error do you get?

I don't under stand how to use

$strArray = explode(' ', strip_tags(trim($str)));
$price = trim($strArray[1]);

in the code
can u put it there in context
cause I tries\d to put it after the $str line
and it just printed a word thats wrong

lets make it easier
I need to get the two lowest prices from this page:

http://www.zap.co.il/model.aspx?modelid=35860

it is in hebrew so the currencuy is the symbol ₪
(I said $ before to make it easier for you)

this is my code:
(note that putting spaces befor or after ([₪]) gives different resolts

<?php

$url = 'http://www.zap.co.il/model.aspx?modelid=35860' ;

$str = file_get_contents($url);

preg_match_all('/([₪])* (\d+(:?.\d+)?)/', $str, $matches, PREG_SET_ORDER);

$i=0;
foreach ($matches as $val) {
   
	if($i==0) $first=$val[2] ;
	if($i==3) $second=$val[2] ;

$i++;

}
$bad_symbols = array(",", "."); $first = str_replace($bad_symbols, "", $first); 
$bad_symbols = array(",", "."); $second = str_replace($bad_symbols, "", $second); 


echo $first . "</br>";
echo $second;



?>

it should print 86 for $first
and 114 for $second
just like the two first numbers at the source code near ₪ symbol

it used to work until they made some changes in thier site
p.s. I called the site to make sure it is legal and they said it is ok

Avukadomusic,

In your post #7 in this topic you said :

I mean the echo $str gives me '798 :price $'
but when I manualy read the source code it is
' price: <span>
798 $</span> '

Is this the entire $str at that point in the code or have you posted an extract from a longer string (ie the whole page)?

Airshow

echo $str gives ofcourse all the page

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.