Page 2 of 2

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

Posted: Wed Oct 14, 2009 5:35 am
by jackpf
No, I've given you the code!!

Do you know what an array is? The code I posted works.

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

Posted: Wed Oct 14, 2009 5:44 am
by simonmlewis
Thanks - well I have absolutely no way of knowing how your code interprets into how to perform what I want.

I need to extract the digits to a variable - so $thedigits contains, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, or 15 from that $contentsstock script.

If you would kindly explain exactly how yours does that, perhaps by echoing something to a screen to prove your theory, I'd be grateful.

From what I can read, it provides only a long array of text - not a single set of digits.

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

Posted: Wed Oct 14, 2009 6:53 am
by jackpf
$romanstock = "http://www.remoteprice.com/data.asp?sto ... 456&type=2";
$contentsstock = file_get_contents($romanstock);

preg_match('/var cText = \'([0-9]+)\'/', $contentsstock, $matches);
echo $matches[1].' <----- this is the number.';


That should show you. Although I can't test it - I'm at college atm.

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

Posted: Wed Oct 14, 2009 7:15 am
by simonmlewis
Now *that* I can understand. Thank you - it works a treat.
Still don't quite understand HOW it actually does it.... so when you have a min, if u could explain that would be good.

But above all - it works.

:)

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

Posted: Wed Oct 14, 2009 7:17 am
by jackpf
What part do you not understand?

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

Posted: Wed Oct 14, 2009 8:03 am
by simonmlewis

Code: Select all

preg_match('/var cText = \'([0-9]+)\'/', $contentsstock, $matches);
echo $matches[1].' <----- this is the number.';
I understand the method to extract what is in the URL, but I don't understand preg_match. I did read about it, but failed to 'get it'.

I also don't understand how $matches[1] works - is that part just showing the 'integer' side of the match?

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

Posted: Wed Oct 14, 2009 12:22 pm
by jackpf
preg_match() uses regular expressions. They take some getting used to...but are pretty straight forward.

$matches is an array of all the matches found from the regex. That thing you thought was a "string array" or whatever, was infact just a representation of what the array consisted of...which is
Array ( [0] => var cText = '10' [1] => 10 )

So $matches[0] contains var cText = '10', which is just the whole string. $matches[1] contains whatever it matched in the parenthesis, which was 10. :)

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

Posted: Wed Oct 21, 2009 1:09 pm
by simonmlewis
The problem has arisen again.
The SCRIPT itself is outputting '25+'.

I am trying to find anything over '1', but this figure is clearly not a 'whole number' so it won't produce a result.

I have set the query to ask for things that are > 1 OR... == "25". I have also tried == "25+", but each time it produces nothing.

Can you offer any support please?

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

Posted: Wed Oct 21, 2009 2:45 pm
by jackpf
I don't follow.

What's your code? And where are you getting '25+' from? Is this what the javascript is producing?

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

Posted: Wed Oct 21, 2009 2:48 pm
by simonmlewis
The code is within this thread.

Normally it's 1, 2, 3, 4, 5...etc. But if the stock level in the system is over 25, ie 100... it just renders 25+.

So I need to query that 25+, but as yet, I cannot see how, based on the final threads of this topic.

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

Posted: Thu Oct 22, 2009 1:36 pm
by jackpf
So the script is producing

Code: Select all

var cText = '25+';
?
Well...25 isn't less than 25...so how is that a problem.

I don't think I'm understanding you...sorry for being stupid :)

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

Posted: Thu Oct 22, 2009 1:43 pm
by simonmlewis
No it's ok.

I am looking for anything greater than 1.
"25+"... is not greater than 1. If the var text is "25+"... then it won't work.

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

Posted: Thu Oct 22, 2009 2:21 pm
by AbraCadaver
simonmlewis wrote: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.
Once you fix the parse error? Still no. You can't use a regex pattern in strpos()! :crazy:

The solutions provided here work, but the way you're thinking about it is hanging you up. It seems that instead of getting the number into another variable that you can use in your if statements, you want to strip out all but the number in $contentsstock and use that?

Try this:

Code: Select all

$contentsstock = preg_replace("/[^0-9]+/", "", $contentsstock);
-Shawn