PHP IF with wildcard
3 posts by 2 authors in: Forums > CMS Builder
Last Post: December 23, 2008 (RSS)
Hi,
Just a quick question, what is the format for wildcards in a PHP IF?
For example I have
So this is defining the variable 'submitted' from the url that is passed.
And defining the variable 'answer' as 'abc'
This works fine.
But I want the IF to work where answer IS LIKE 'submitted'
I have tried all variations using LIKE, % and * but nothing works. For example, this doesn't work...
But then I realised that LIKE and % is SQL, not php! And i've tried googling php IF wildcards, but to no avail, which suggests that it's not actually that simple!
Any ideas would be most apprecaited,
Cheers, and merry christmas
Rob
Just a quick question, what is the format for wildcards in a PHP IF?
For example I have
$submitted = strtolower($_GET['submitted']);
$answer = 'abc';
<?php if ($answer == ($submitted) ): ?>
So this is defining the variable 'submitted' from the url that is passed.
And defining the variable 'answer' as 'abc'
This works fine.
But I want the IF to work where answer IS LIKE 'submitted'
I have tried all variations using LIKE, % and * but nothing works. For example, this doesn't work...
<?php if ($answer LIKE (%$submitted%) ): ?>
But then I realised that LIKE and % is SQL, not php! And i've tried googling php IF wildcards, but to no avail, which suggests that it's not actually that simple!
Any ideas would be most apprecaited,
Cheers, and merry christmas
Rob
Re: [rjbathgate] PHP IF with wildcard
By Dave - December 22, 2008
Hi Rob,
Yes, it's totally different in PHP then in MySQL. There's a list of string functions here: http://ca.php.net/manual/en/ref.strings.php
And much more advanced pattern matching functions called "Regular Expressions" that you can learn about that let you do this kind of thing (google for details).
But to keep it simple for now I'd just use this function: http://www.php.net/stripos
Try this:
<?php if (stripos("abc", $_GET['submitted']) !== false): ?>
Found submitted value in "abc" string.
<?php endif ?>
Basically it looks for the second value in the first value. If it find it it returns the offset (0, 1, 2, etc) or false if no match. Hope that helps, let me know if you need more details.
Yes, it's totally different in PHP then in MySQL. There's a list of string functions here: http://ca.php.net/manual/en/ref.strings.php
And much more advanced pattern matching functions called "Regular Expressions" that you can learn about that let you do this kind of thing (google for details).
But to keep it simple for now I'd just use this function: http://www.php.net/stripos
Try this:
<?php if (stripos("abc", $_GET['submitted']) !== false): ?>
Found submitted value in "abc" string.
<?php endif ?>
Basically it looks for the second value in the first value. If it find it it returns the offset (0, 1, 2, etc) or false if no match. Hope that helps, let me know if you need more details.
Dave Edis - Senior Developer
interactivetools.com
interactivetools.com
Re: [Dave] PHP IF with wildcard
Hey,
Excellent thanks Dave - I was trying with 'regular expressions' but something with my format must've been wrong as I couldn't get that to work either!
Stripos is perfect for now, so thanks!
Merry Christmas
Rob
Excellent thanks Dave - I was trying with 'regular expressions' but something with my format must've been wrong as I couldn't get that to work either!
Stripos is perfect for now, so thanks!
Merry Christmas
Rob