I would like to know how to check with regex or whatever if a number begins with 0 or 00
I did so but I'm newbie at regex:
preg_match("/^[0][0-9]+$/", $number)
You may try the below code
if (preg_match("~^0\d+$~", $number)) {
// Yes
}
else {
// no
}
Explanation:
^ the beginning of the string
0 '0'
\d+ digits (0-9) (1 or more times)
$ before an optional \n, and the end of the
string
DEMO
^(?:0|00)\d+$
Try this.See demo,
http://regex101.com/r/rQ6mK9/16
This problem doesn't really require regex. Just use substr function:
if (substr($str, 0, 1) == '0' || substr($str, 0, 2) == '00') {
//=> matched
}
Related
I have a function:
function Validate($name)
{
$rename = 'Rename' .$name;
if (strlen($rename) > 50) {
$rename = substr($rename, 0, 48) . '..';
}
return $rename;
}
The function is called as follows:
$data['name'] = Validate($duplicate->name."_").$i++;
If name is 50 characters then it is cut-shortened to 48 characters and extra .. at the end . In case if the name is 50characters ending with .._somedigits.
I would like to do $rename = substr($rename, 0, 45) . '..'.$suffix; I would like to have this extra check with sffix
Any help would be appreciated.
You are concatenating $rename = 'Rename' .$name; and then you get the strlen based on that $rename. So that adds "Rename" to the length of the string.
If that is you intention, you could use a regex _\d+$ to check if the string ends with and underscore and one or more digits.
function Validate($name)
{
$suffix = "suffix";
$rename = 'Rename' . $name;
$re = '/_\d+$/';
if (strlen($rename) > 50) {
if (preg_match($re, $rename)) {
return substr($rename, 0, 45) . '..' . $suffix;
}
return substr($rename, 0, 48) . '..';
}
return $rename;
}
Demo
You can use the regex ^.*_0$
If this regex matches then you can make substring to 45 characters.
Reg
$strings = [
'abcdefghijklmnopqrstuvwxyz',
'abcdefghijklmnopqrstuvwxyz_12',
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx',
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz',
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz_12',
];
foreach($strings as $str) {
if (preg_match('/_\d+$/', $str)) {
echo preg_replace('/^(?=.{51}).{45}\K.+(_\d+)$/', '..$1', $str),"\n";
} else {
echo preg_replace('/^(?=.{51}).{48}\K.+$/', '..', $str),"\n";
}
}
Output:
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz_12
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuv..
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs.._12
First regex explain:
^ : beginning of string
(?=.{51}) : lookahead, 0-length assertion that makes sure we have, at least, 51 characters
.{45} : exactly 45 characters
\K : forget all we have seen until this position
.+ : 1 or more any character
(_\d+) : group 1, underscore and last digits
$ : end of line
Second regex explain:
^ : beginning of string
(?=.{51}) : lookahead, 0-length assertion that makes sure we have, at least, 51 characters
.{48} : exactly 48 characters
\K : forget all we have seen until this position
.+ : 1 or more any character
$ : end of line
I want to remove brackets and number from beginning of string but problem is brackets and number is coming only specific string not all.
for example following is my string.
1) [4] Mustangs 8u
2) Pool a First Place
3) Team slect
4) [3] In pruduct
so above you can see only 1 and 4 string have number with brackets at beginning of string so i want to only remove that if that found in string.
I write following code but is not work.
<?php
foreach ($grouped as $round_number => $group) {
$team_1_name = $group->team_1_name;
$new_str = preg_replace('/^([0-9]* \w+ )?(.*)$/', '$2', $team_1_name);
$date = date ('F d, Y g:iA', $unix_time);
}
?>
Try regular expression /^(\[[0-9]\]?\s?)/ as:
$new_str = preg_replace('/^(\[[0-9]\]?\s?)/', '$2', $team_1_name);
For reference: regexr
In case your numbers are multi digit (i.e. '[11] In pruduct')...
echo preg_replace('/^(\[\d*\]?\s?)/', '$2', $team_1_name);
Instead regex you can use ltrim() with a character mask. If your strings never start with a number:
$new_str = ltrim($team_1_name, "0123456789[] ");
else you could check if the first char is a bracket:
$new_str = $team_1_name[0] == '[' ? ltrim($team_1_name, '0123456789[] ') : '';
I would check a string if contains at least:
2 letters (a-z) CASE INSENSITIVE
4 digits (0-9)
the order is not important. It could be a1234b, abcd1234, 4444aa etc etc.
My actual regex is
if (preg_match("/[a-z][^a-z]*[a-z]*[0-9]/i",$string)) {
echo 'secure';
$continue = true;
}
and it doesn't function if string start with a digit. Thank you
^(?=(?:.*[a-zA-Z]){2})(?=(?:.*[0-9]){4})\w+$
You can use lookahead here to apply the conditions.See demo.
https://regex101.com/r/vV1wW6/22
$re = "/^(?=(?:.*[a-zA-Z]){2})(?=(?:.*[0-9]){4})\\w+$/m";
$str = "a1234b\nabcd1234\n4444aa\n12\n12a\n12aa22";
preg_match_all($re, $str, $matches);
You can use a human-readable check like this:
Remove all non-letters (\P{L} for letters and \P{M} for non-diacritics) and if the resulting string length is less than 2, return false
Remove all non-digits (\D) and if the resulting string length is less than 4, return false.
As for
it doesn't function if string start with a digit
You can add an if (preg_match('/^\D/', $str)) where ^\D means starts with a non-digit.
PHP code:
if (preg_match('/^\D/', $str)) {
if (mb_strlen(preg_replace('/\D/', '', $str), 'UTF8') < 4 ||
mb_strlen(preg_replace('/[\P{L}\P{M}]/u', '', $str), 'UTF8') < 2) {
echo "Invalid";
}
else
{
echo "Valid";
}
}
See IDEONE demo
Note that to count the length of a Unicode string you need mb_strlen function.
You can try this pattern (\b[a-zA-Z]+\d{4}[a-z]?|\b[a-zA-Z]+\d{4}|\b\d{4}[a-zA-Z]+)
See demo here https://regex101.com/r/uJ0vD4/8
Input
a1234b, abcd1234, 4444aa, 33333, ab555, aac3566
Output
a1234b, abcd1234, 4444aa, aac3566
^(?=(?:.*[0-9]){4})\w+$ works for this case
for PHP:
$re = "/^(?=(?:.*[0-9]){4})\\w+$/m";
$str = "a1234b\naaa32233\n";
preg_match_all($re, $str, $matches);
for javascript:
var re = /^(?=(?:.*[0-9]){4})\w+$/gm;
var str = 'a1234b\naaa32233\n';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
how can I match a the first number next to a % character ?
<?php
$string = 'Get 30% off when you spend over £100 on electronics';
if(strpos($string,'% off') !== false) {
$number = preg_replace("/[^0-9%]/", '', $string);
return $number;
}
this returns 30%100
any help would be great thanks in advance.
This regex will match (and capture) all digits preceding a '%' sign:
'/(\d+)%/'
You can try something like this:
$string = 'Get 30% off when you spend over £100 on electronics';
preg_match('/(\d+)%/', $string, $matches);
print_r($matches[1]);
Do let us know if your requirements are more complex.
Regex:
\d{1,3}%
Explained:
\d{1,3} match a digit [0-9]
Quantifier: {1,3} Between 1 and 3 times, as many times as possible, giving back as needed [greedy]
% matches the character % literally
This seems to do the trick :)
if(strpos($string,'%') !== false) {
$regex_percent = "/((\d{1,5})(?:%))/";
preg_match($regex_percent, $string, $matches_off);
$number = $matches_off[2];
return $number;
}
Hey could someone help me to test if a string matches 3 double digit figures separated by a colon? For Example:
12:13:14
I understand I should be using preg_match but I can't work out how
Preferably the first number should be between 0 and 23 and the second two numbers should be between 0 and 59 like a time but I can always work that out with if statements.
Thanks
This answer does correct matching across the entire string (other answers will match the regexp within a longer string), without any extra tests required:
if (preg_match('/^((?:[0-1][0-9])|(?:2[0-3])):([0-5][0-9]):([0-5][0-9])$/', $string, $matches))
{
print_r($matches);
}
else
{
echo "Does not match\n";
}
You could use preg_match with number comparissons on $string = '23:24:25';
preg_match('~^(\d{2}):(\d{2}):(\d{2})$~', $string, $matches);
if (count($matches) != 3 || $matches[1] > 23 || $matches[2] > 59 || $matches[3] > 59 ......)
die('The digits are not right');
Or you can even ditch the regular expresions and use explode with numeric comparisons.
$numbers = explode(':', $string);
if (count($numbers) != 3 || $numbers[0] > 23 || $numbers[1] > 59 || $numbers[2] > 59 ......)
die('The digits are not right');
$regex = "/\d\d\:\d\d\:\d\d/";
$subject = "12:13:14";
preg_match($regex, $subject, $matches);
print_r($matches);
if (preg_match ('/\d\d:\d\d:\d\d/', $input)) {
// matches
} else {
// doesnt match
}
\d means any digit, so groups of two of those with : in between.