Page 1 of 2

Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 7:08 am
by simonmlewis

Code: Select all

$romanstock = "http://www.remoteprice.com/data.asp?storeid=123&itemcode=456&type=2";
$contentsstock = file_get_contents($romanstock);
The above code, when rendering $contentsstock, renders this:
var cText = '13' document.write(cText)
'13' could be 1 or even 189.

I need to get those digits out of there, and then query them, ie;

Code: Select all

if ($contentsstock >= "2") { echo "this is 2";}
I think I know how to strip out characters from character 10 (for example) up to character 12, but this quote could be 1, 2 or 3 digits.

Can anyone help me here please?

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 7:21 am
by jackpf
Could try:

preg_match('/var cText = \'([0-9]+)\'/', $str, $matches);
print_r($matches);

I didn't use code tags because it removes the backslashes in the regex :/ FYI :D

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 7:58 am
by simonmlewis
Sorry, I am not quite with you..

Code: Select all

     $romanstock = "http://www.remoteprice.com/data.asp?storeid=123&itemcode=456&type=2";
      $contentsstock = file_get_contents($romanstock);
      
      preg_match('/var cText = \'([0-9]+)\'/', $str, $matches);
print_r($matches);
Do you mean like this?
I thought the 'preg_match' script would want the $contentsstock in it, rather than hardcode?

This produces:
Array ( ) var cText = '13' document.write(cText)

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 9:41 am
by jackpf
:|

It was an example. Yes, put your own variables in.

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 9:50 am
by simonmlewis
Sorry - didn't mean to sound blunt (not having a great day!).

I have tried putting the $variable in, but I had errors asking for ; or ,
I think the '\ and /' was causing problems. How should I be writing this?

I can see how preg_match should work. It looks in the result for a string, 0-9 stating it to be numeric, and then returns that match to an output variable. Just cannot see how to make it work with a variable without the errors.

Code: Select all

     $romanstock = "http://www.remoteprice.com/data.asp?storeid=123&itemcode=456&type=2";
      $contentsstock = file_get_contents($romanstock);
      
      preg_match($contentsstock([0-9]+)\'/', $str, $matches);
print_r($matches);
produces...
Parse error: parse error, expecting `')'' in C:\xampp\phpmyadmin\site\includes\product.inc on line 127
Line 127 is

Code: Select all

     preg_match($contentsstock([0-9]+)\'/', $str, $matches);

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 9:55 am
by Mark Baker
preg_match('/var cText = '([0-9]+)'/', $contentsstock, $matches);

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 9:59 am
by simonmlewis

Code: Select all

     $romanstock = "http://www.remoteprice.com/data.asp?storeid=123&itemcode=456&type=2";
      $contentsstock = file_get_contents($romanstock);
      
      preg_match('/var cText = '([0-9]+)'/', $contentsstock, $matches);
print_r($matches);
This is producing the same error on the same line.
Parse error: parse error in C:\xampp\phpmyadmin\site\includes\product.inc on line 127
Again, line 127 is the preg_match line.

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 11:33 am
by jackpf
Yeah, the stupid syntax highlighter removes the backslashes. So \' becomes '. That's what's breaking it.

This is what it should be:
preg_match('/var cText = \'([0-9]+)\'/', $contentsstock, $matches);

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 11:43 am
by simonmlewis
Mmmmm that is producing this in the print_r($matches); rendering:
Array ( [0] => var cText = '10' [1] => 10 )
No errors of script as such.

Code: Select all

     $romanstock = "http://www.remoteprice.com/data.asp?storeid=123&itemcode=456&type=2";
      $contentsstock = file_get_contents($romanstock);
      
      preg_match('/var cText = \'([0-9]+)\'/', $contentsstock, $matches);
print_r($matches);

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 11:44 am
by jackpf
And..?

Isn't that what you wanted? :P

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 11:57 am
by simonmlewis
How do I establish if the number is greater than 0 and less than 15?
It's picking out a chunk of code and text - not the one figure from that query - which was my initial problem.

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 12:00 pm
by jackpf
No, it's returning an array of the matches.

Code: Select all

if($matches[1] > 0 && $matches[1] < 15)
//...

Re: Finding digits in variable containing text, and IDing them

Posted: Tue Oct 13, 2009 12:11 pm
by simonmlewis
I'm sorry, I really do not understand.
My request is to find a way to extract the numbers from the http result, and then once I have that number, I can query it separately.

Re: Finding digits in variable containing text, and IDing them

Posted: Wed Oct 14, 2009 3:42 am
by simonmlewis
I need to extract the numbers from within the results, so I can query it. The numbers are a stock level. If the number is below 15 and higher than 0, I want to say something.

Re: Finding digits in variable containing text, and IDing them

Posted: Wed Oct 14, 2009 5:13 am
by simonmlewis
Would this work?

Code: Select all

 
$romanstock = "http://www.remoteprice.com/data.asp?storeid=44175&itemcode=40703&type=2";
$contentsstock = file_get_contents($romanstock);
if(strpos($contentsstock, "([0-9]+)") >= 1 && <=15) { echo "success";}
 
I think preg_match just returns an array of text, whereas I want to return just the numbers inside the $contentsstock result.