is it possible to use wildcard in an if statement?
My code:
*=wildcard
if ($order_info=='Quatro*') {
}
$order_info will be "Quatro - na splátky", or "Quatro - čťžýáí"
Use regex:
if (preg_match('/^Quatro/', $order_info)) {
}
or strpos:
if (strpos($order_info, 'Quatro') === 0) {
}
Edit: Avoiding regex engine invocation for simple string matches like this is usually preferred. strpos will do the same job less expensively.
Sure, use a regex:
if( preg_match( '/^Quatro.*/', $order_info))
{
}
No. Use preg_match or strpos instead.
Regular expressions will do this.
Or you could do:
if($order_info.substr(0, 6) == 'Quatro')
Related
I have some words with | between each one and I have tried to use preg_match to detect if it's containing target word or not.
I have used this:
<?php
$c_words = 'od|lom|pod|dyk';
$my_word = 'od'; // only od not pod or other word
if (preg_match('/$my_word/', $c_words))
{
echo 'ok';
}
?>
But it doesn't work correctly.
Please help.
No need for regular expressions. The functions explode($delimiter, $str); and in_array($needle, $haystack); will do everything for you.
// splits words into an array
$array = explode('|', $c_words);
// check if "$my_word" exists in the array.
if(in_array($my_word, $array)) {
// YEP
} else {
// NOPE
}
Apart from that, your regular expression would match other words containing the same sequence too.
preg_match('/my/', 'myword|anotherword'); // true
preg_match('/another/', 'myword|anotherword'); // true
That's exactly why you shouldn't use regular expressions in this case.
You can't pass a variable into a string with single quotes, you need to use either
preg_match("/$my_word/", $c_words);
Or – and I find that cleaner :
preg_match('/' .$my_word. '/', $c_words);
But for something as simple as that I don't even know if I'd use a Regex, a simple if (strpos($c_words, $my_word) !== 0) should be enough.
You are using preg_match() the wrong way. Since you're using | as a delimiter you can try this:
if (preg_match('/'.$all_words.'/', $my_word, $c_words))
{
echo 'ok';
}
Read the documentation for preg_match().
I was wondering if anybody knew if there was a way to concatenate a * (all) to a string inside an if (or switch) statement. For example if you had a URL called /hello/there and /hello/whats-up ... is there anyway you could have something like the following:
if ($url="/hello/" . *) {
sayHello();
} else { sayGoodebye(); }
etc... I don't think that's the correct syntax, but if anybody knows what I'm talking about it would be a great help.
Thanks (:
$match = "/hello/";
if (substr($url, 0, strlen($match)) === $match) {
sayHello();
} else {
sayGoodbye();
}
Do not use regular expressions if you don't have to...
You can also check for the position of $match in the $url string:
$match = "/hello/";
if (strpos($url, $match) === 0) {
sayHello();
} else {
sayGoodbye();
}
Use regular expressions:
http://www.regular-expressions.info/php.html
So it would be something like this:
if (ereg("/hello/", $url)) {
sayHello();
} else { sayGoodebye(); }
Although that would match anything with "/hello/" in it anywhere, so if you only wanted to match strings that start with "/hello/" you'd have to modify the expression. The point is, if you've never used regular expressions it's a good thing to invest some time into. It'll pay off eventually because at some point you're going to need this skill.
Edit: I'll leave my original code here as reference, but please see phihag's comments and use preg_match and the preg_match compatible expression instead of ereg and the expression I used.
I have to check if a variable (php, preg_match) is from 1988 and 2011 using regex; I know how to do it with normal if/else, but I'd like to use regex for this!
Sometimes, regular expressions are not the only answer:
if (preg_match('/^\d{4}$/', $input) && $input >= 1988 && $input <= 2011) {
}
Wouldn't be that easy, as regex is meant to match character by character. You could use something like this (probably wouldn't be a good idea).
/(198[89]|199\d|200\d|201[01])/
Why do you want to do this using regex?
One solution could be something along the lines of (?:198[8-9]|199[0-9]|200[0-9]|201[0-1]).
Use preg_replace_callback :
<?php
preg_replace_callback('%([0-9]{4})%', function($match) {
$number = $match[1];
if($number < 1988 || $number > 2011) return; /* Discard the match */
/* Return the replacement here */
}, $input);
This is in my opinion the most flexible solution.
Try this:
/^[12][90][8901][8901]\z/
An example would be "SU1203" or "UP1234" or any two letters followed by numeric values.
thanks
This can be solved with the quite simple expression
^[A-Z]{2}\d+$
In JavaScript you can use the test() method:
if(/^[A-Z]{2}\d+$/.test(str))
and in PHP, preg_match:
if(preg_match('/^[A-Z]{2}\d+$/', $str) === 1)
I suggest to learn regular expressions.
See also:
Regular expressions in JavaScript
Regular expressions in PHP
Try:
if (preg_match('|^[A-Z]{2}[0-9]+$|', $string_to_test))
{
// matched
}
javascript example:
if ( /(^[A-Z]{2}\d+)/.test('SU1203') ) {
alert( 'matched' )
} else { alert('not matched') }
I'm converting one of my old Perl programs to PHP, but having trouble with PHP's string handling in comparison to Perl.
In Perl, if I wanted to find out if $string contained this, that or the_other I could use:
if ($string =~ /this|that|the_other/){do something here}
Is there an equivalent in PHP?
You can either use a regular expression (e.g. preg_match):
if(preg_match('/this|that|the_other/', $string))
or make it explicit (e.g. strstr):
if(strstr($string, 'this') || strstr($string, 'that') || strstr($string, 'the_other'))
You can use PHP's preg_match function for a simple regex test.
if ( preg_match( "/this|that|the_other/", $string ) ) {
...
}
In PHP you can use preg_match function as:
if( preg_match('/this|that|the_other/',$string) ) {
// do something here.
}