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/
Related
Is there a simple way to use ltrim() to remove a single instance of a match instead of all matches?
I'm looping through array of strings and I'd like to remove the first, and only first, match (vowels in this case):
ltrim($value, "aeiouyAEIOUY");
With default behavior the string aardvark or Aardvark would be trimmed to be "rdvark". I'd like result to be "ardvark".
I'm not bound to ltrim by any means but it seemed the closest built-in PHP function. It would be nice of ltrim and rtrim had an optional parameter "limit", just saying... :)
Just use preg replace it has a limit option
eg
$value = preg_replace('/^[aeiouy]/i', '', $value, 1);
Regular expressions is probably overkill, but:
$value = preg_replace('/^[aeiouy]/i', '', $value);
Note the i makes it case-insensitive.
You can't use ltrim to do this for the reasons you say, nor can you use str_replace (which also has no limit). I think it's easiest just to use a regex:
$value = preg_replace('/^[aeiouy]/i', '', $value);
However if you really don't want to do that, you can use a substring, but you would have to check the position of any of those strings in the string in a loop as there is no php function that does such a check that I know of.
You can use the preg_replace function:
<?php
$value = preg_replace('/^[aeiouy]/i', '', $value);
?>
There are several way you can go about doing what you are looking to do.
Perhaps most straightforward would be a regular expression replacement like this:
$pattern = '/^[aeiouy]{1}/i';
$result = preg_replace($pattern, '', $original_string);
This is probably the most efficient way (so ignore my regular expressions answer):
if (strpos('aeiouyAEIOUY', $value[0]) !== false) $value = substr($value, 1);
Or,
if (stripos('aeiouy', $value[0]) !== false) $value = substr($value, 1);
I would like to check if a URL (or any string) contains any form of the following pattern ####-##-##
Does anyone have a handy str_replace() or regular expression to do that?
something like:
contains_date_string($string);
returns true if $string contains ####-##-##
Thank you!!!
if (preg_match('/\b\d{4}-\d{2}-\d{2}\b/', $str)) {
// ...
}
If the word boundary (\b) doesn't do the trick, you could try negative lookbehind and lookaheads:
if (preg_match('/(?<!\d)\d{4}-\d{2}-\d{2}(?!\d)/', $str)) {
// ...
}
As an additional validation, you could use checkdate() to weed out invalid dates such as 9999-02-31 as mentioned in this answer.
Use preg_match in conjunction with checkdate:
function contains_date($str)
{
if (preg_match('/\b(\d{4})-(\d{2})-(\d{2})\b/', $str, $matches))
{
if (checkdate($matches[2], $matches[3], $matches[1]))
{
return true;
}
}
return false;
}
'/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/'
'Makes sure things like 1998-13-32 won't get past and validate.'
I got this from google... http://www.devnetwork.net/viewtopic.php?f=29&t=13795
Looks promising. Hope this will help someone on the search for the same as stackoverflow is the most accessible SEO wise.
The test given here should work:
if (preg_match('#[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])#', $str)) {
// do something
}
Whatever regex you choose, be careful! When a machine sees 2004-04-01, it won't be able to distinguish between January 4th and April Fools day unless you tell it otherwise...
I have strings like this : "ABC/ABC/123", "ABC/ABC/123/567", "ABC/123"
And I want to search the strings that have 2 ore more "/"
How can I do that with regular expression ?
EDIT
I have found a solution like this:
´if($sku1!=$sku2 && preg_match("#^".$sku1."[/]#", $sku2) && substr_count($sku2, '/')==1) {´
Any improvement?
Thanks for help.
I agree with adrien: you have every reason not to use regex for this, though I'd suggest another solution:
$string = 'ABC/ABC/123';
if (count(explode('/',$string)) > 2)
{
echo $string.' contains at least 2 slashes';
}
EDIT
As the OP pointed out in his comment below: substr_count($string,'/') is the easiest and fastest way to check for 2+ slashes...
Yes you can use the regex:
^([^\/]+\/){2,}$
Without regexp (so much faster) :
$string = "ABC/ABC/123";
if (strpos($string, '/', strpos($string, '/') + 1) !== FALSE) {
echo "2 or more slashes in this string!";
}
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')
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.
}