Finding digits in variable containing text, and IDing them

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

No, I've given you the code!!

Do you know what an array is? The code I posted works.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.

:)
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

What part do you not understand?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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. :)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

I don't follow.

What's your code? And where are you getting '25+' from? Is this what the javascript is producing?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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 :)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply