Forum Moderators: coopster

Message Too Old, No Replies

Need If statement for all numbers plz

         

ElMobd3

12:21 am on Jul 27, 2010 (gmt 0)

10+ Year Member



Hi,
Plz I need a help in if statement.
I want to include a file when a user enter on a link on my site ending with numbers 0-9.
like that:
*****.com/223
***.com/48
I'm still trying but not get the correct statement.

I used many codes like :

<? if (preg_match("^[0-9]$", $_SERVER['QUERY_STRING'])) include("load.php");?>

<? if ($_SERVER['QUERY_STRING'] =='^[0-9]$') include("load.php");?>


so plz anyone correct the code to me.

Matthew1980

7:12 am on Jul 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there ElMobd3,

Welcome to WebmasterWorld :) [webmasterworld.com ]

Firstly:

When doing/using PHP, for ease of transportability between servers (especially if you migrate) use the full tags (<?php ?>) as this makes the code workable when changing servers, because this is set in the php.ini file and not all servers/hosts support the use of short tags, and doing the full tags can save headaches further down the line :)

Secondly:

if(preg_match("/^[0-9]$/", $_SERVER['QUERY_STRING'])){
//matches numbers in the url
}

I think that's something like you need. Have a read through the manual to see the options [uk3.php.net ]

Hope that helps,

Cheers

ElMobd3

12:02 pm on Jul 27, 2010 (gmt 0)

10+ Year Member



Thank you Matthew1980

But can I use $_SERVER['QUERY_STRING'] instead of preg_match ?
like that
<?php if($_SERVER['QUERY_STRING']=="/^[0-9]$/")   Do That ?>

I tested it but didn't get anything!


Another help please
if I have links like those:
example.com/?id=33
example.com/?id=234
example.com/?id=245

I need If statement to check if the like ending with ?id=numbers

Matthew1980

12:25 pm on Jul 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there ElMobd3,

The if clause with the preg_match pattern in it will or should work because you are trying to match a specific pattern, so just using: if($_SERVER['QUERY_STRING'] == "/^[0-9]$/") wouldn't work anyway, as the syntax is invalid - well technically it's not, but preg wise it's wrong :)

Are you saying that you tested the snippet that I posted, and it hasn't functioned? If so, make sure that the $_SERVER['QUERY_STRING'] has actually got the values in it that you are expecting, ie echo it to screen:-

<?php
//echo the global to make sure that the relevant data is there
echo $_SERVER['QUERY_STRING'];
?>

Because if the data contained within the global is different to the pattern you are checking for, it would never match, therefore the clause would never evaluate to true.

From the example you are giving you are just wanting to capture the id values from the query string? If that's the case, you only need to use $_GET['id'] because the $_GET['id'] will equal (if set) the value assigned to it, hope that makes sense.

Example:

Imagine this url in the address bar:-

example.com/?id=33

<?php
//script to catch ID number
if(isset($_GET['id'])){

echo "Your ID number is:".$_GET['id'];

}else{
echo "No value found in url";
exit;
}
?>

This would output to screen:-

Your ID number is:33

So realistically you only need to check the get, check that it's numerical (use preg_match("/^[0-9]$/", $_GET['id']) for that) and then from that you can use that captured data for whatever you need.

Hope that makes sense to you :)

Cheers,
MRb

ElMobd3

2:51 pm on Jul 27, 2010 (gmt 0)

10+ Year Member



Thank you Matthew1980, that's help me so much.
Thanks again.

rocknbil

6:00 pm on Jul 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your problem is you are saying "begins with and ends with ONLY", and only ONE DIGIT. Look:

*****.com/223
***.com/48


<?php if (preg_match("^[0-9]$", $_SERVER['QUERY_STRING'])) include("load.php");?>

^ This means "the string starts with"
$ this means "the string ends with"

And it's one number, and only one number. Add the + quantifier for "one or more." (* means zero or more, which becomes important in a minute . . . )

Note that 0-9 and \d are equivalent, you don't need a class [] if you use \d.

A quick fix, remove the ^, add the +.

<?php if (preg_match('/\d+$/', $_SERVER['QUERY_STRING'])) include("load.php");?>

This would mean "only ends with one or more numbers." But there may be other problems, some browsers add / on the end in links, and there may be some condition where a digit may appear and mess up your scheme, like testversion2 or something. A little more specific, insuring it matches only on numbers after the last /,

<?php if (preg_match('/[^\/]+\/\d+\/*$/', $_SERVER['QUERY_STRING'])) include("load.php");?>

Should work for

example.com/something/something-else/1234
example.com/something/something-else/1234/
example.com/1234
example.com/1234/

I know I said lose the ^, but that character has different meanings in different contexts. By itself, at the beginning of a pattern, it means "string begins with" but within a class, if the first character in the class, it means "anything NOT these." So an examination,

[^\/]+ --> one or more of any character NOT a slash
\/ --> followed by a single slash
\d+ --> followed by one or more digits
\/* --> followed by ZERO or more slashes
$ --> followed by the absolute end of the string.

Matthew1980

6:41 pm on Jul 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I must hold my hands up, I am still learning the regex stuff, when I originally posted I thought as I had got the basic digit check correct, from what Rocknbil has written I was close, but no cigar :( I was missing the + sign. Doh!

I'm sure that the $_GET['id'] would do exactly the same thing though, just that the regex version is much more precise - ie, you can set the patterns to only match when explicit conditions are met - I understand the logic, but struggle with the syntax :)

Thank you Rocknbil for the regex "how to" there. But honestly I am trying to learn it :-p

Cheers,
MRb

ElMobd3

1:52 pm on Jul 28, 2010 (gmt 0)

10+ Year Member



I've used the first code because it work for only numbers form 0 to 9

But I edited it to
<?php if(preg_match("/^[0-9]{1,5}$/", $_SERVER['QUERY_STRING']))include("load.php");?> and it's work perfectly
But I think your code It's more efficient, I'll use it

Thank you both Rocknbil and Matthew1980