PHP - preg_match for only numbers and single dash - php

Let's say I only allow this kind of format 2015-2016 which contains number and only one dash. How can I do this with preg_match? I tried the following, but with no luck.
$a = '2015-2016';
if(!preg_match('/[^0-9\-]/i',$a)) {
then return not valid data`
}

Hope this will help
preg_match('/^\d{4}-\d{4}$/', $string);
^ Start of the string
\d{4} match a digit [0-9] Exactly 4 times
- matches the character - literally
\d{4} match a digit [0-9] Exactly 4 times
$ End of the string

$a = '2015-2016';
if(!preg_match('/^[0-9 \-]+$/',$a)) {
then return not valid data }
Try that.

Related

compare two price strings in php with regex

Say we have two price strings in different format:
$s_price = '85.95' or '1500.00'
$r_price = '$ 85.95' or '1,500'
But all these prices are the same and should match.
I have a regex to do that but don't know if this is how we do it:
(\d+)*(,)?\d+(.)?\d*
To retrieve and parse a float from a string in PHP, use the floatval() method.
For the symbols, it depends on wether you always use the same conventions for your currencies (comma for thousands separator and dot for decimals). In that case, you should remove non-digits except dots with the preg_replace() method (the correspondig Regex could be /[^0-9.]/)
<?php
function sanitize($price) {
return floatval(preg_replace('/[^0-9.]/', '', $price));
}
$a1 = '85.95';
$a2 = '1500.00';
$b1 = '$ 85.95';
$b2 = '1,500';
sanitize($a1); // (float) 85.95
sanitize($a2); // (float) 1500
sanitize($b1); // (float) 85.95
sanitize($b2); // (float) 1500
sanitize($a1) === sanitize($b1); // (bool) true
sanitize($a2) === sanitize($b2); // (bool) true
sanitize($a1) <= sanitize($a2); // (bool) true
sanitize($b1) >= sanitize($b2); // (bool) false
Hope it will help !
You have a lot of optional parts in your pattern using ? and * and you could omit the capturing groups if you are not referring to them in the code.
What you might do is match an optional part for the dollar sign followed by 0+ horizontal whitespace chars.
Then match 1+ digits followed by an optional part to match a dot or comma and 1+ digits:
(?<!\S)(?:\$\h*)?\d+(?:[,.]\d+)\b
Explanation
(?<!\S) Assert what is on the left is not a non whitespace char
(?:\$\h*)? Optionally match a dollar sign and 0+ horizontal whitespace chars
\d+(?:[,.]\d+) Match 1+ digits followed by an optional part to match either a dot or comma and 1+ digits
\b word boundary to prevent the digit being part of a larger word
Regex demo | Php demo
you store numbers as integer or float
and to compare you need to use || not or
hope that was helpful

PHP - Find an integer that is 8 digit long in a string between a range of numbers using preg_replace

I am trying to find an integer in a string that has the following characteristics:
- Is exactly 8 digits long
- Is between 21000000 and 22000000
- Or between 79000000 and 79999999
I want any number between those ranges to be redacted.
I tried using preg_replace. I'm not sure which pattern to use for this function.
I would suggest this:
preg_replace('/(^|[^0-9]{1})(21[0-9]{6}|22000000|79[0-9]{6})([^0-9]{1}|$)/', '$1 |$2| $3', $str);
// (^|[^0-9]{1}) - set bordering character as non-numeric
// (21[0-9]{6}|22000000|79[0-9]{6}) - match the numbers range you need
// ([^0-9]{1}|$) make sure it doesn't include any other numbers
NB! Make sure you include $1 and $3 in your replace string, otherwise you'll lose chars surrounding the number.
try
<?php
$str ="sdsds21000021dsds";
$int = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
if($int){
$number_of_digits = strlen((string)$int);
if($number_of_digits == 8){
if((($int >= 21000000)&&($int <=22000000))||(($int >= 79000000)&&($int <=79999999))){
echo $int;
} else { // not found }
} else { // not found }
} else { // not found }
hope it helps :)
I've managed to build a regular expression based on what you need.
Hope it helps!
\b21[0-9]{6}\b|\b79[0-9]{6}\b
\b is word boundary
{number} is repetition count
21 is interpreted literally, as 79 is
[0-9] matches exactly one number in 0-9 range
test it here please if you need to tweak it.
https://regex101.com/

php regular expression minimum and maximum length doesn't work as expected

I want to create a regular expression in PHP, which will allow to user to enter a phone number in either of the formats below.
345-234 898
345 234-898
235-123-456
548 812 346
The minimum length of number should be 7 and maximum length should be 12.
The problem is that, the regular expression doesn't care about the minimum and maximum length. I don't know what is the problem in it. Please help me to solve it. Here is the regular expression.
if (preg_match("/^([0-9]+((\s?|-?)[0-9]+)*){7,12}$/", $string)) {
echo "ok";
} else {
echo "not ok";
}
Thanks for reading my question. I will wait for responses.
You should use the start (^) and the end ($) sign on your pattern
$subject = "123456789";
$pattern = '/^[0-9]{7,9}$/i';
if(preg_match($pattern, $subject)){
echo 'matched';
}else{
echo 'not matched';
}
You can use preg_replace to strip out non-digit symbols and check length of resulting string.
$onlyDigits = preg_replace('/\\D/', '', $string);
$length = strlen($onlyDigits);
if ($length < 7 OR $length > 12)
echo "not ok";
else
echo "ok";
Simply do this:
if (preg_match("/^\d{3}[ -]\d{3}[ -]\d{3}$/", $string)) {
Here \d means any digits from 0-9. Also [ -] means either a space or a hyphen
You can check the length with a lookahead assertion (?=...) at the begining of the pattern:
/^(?=.{7,12}$)[0-9]+(?:[\s-]?[0-9]+)*$/
Breaking down your original regex, it can read like the following:
^ # start of input
(
[0-9]+ # any number, 1 or more times
(
(\s?|-?) # a space, or a dash.. maybe
[0-9]+ # any number, 1 or more times
)* # repeat group 0 or more times
)
{7,12} # repeat full group 7 to 12 times
$ # end of input
So, basically, you're allowing "any number, 1 or more times" followed by a group of "any number 1 or more times, 0 or more times" repeat "7 to 12 times" - which kind of kills your length check.
You could take a more restricted approach and write out each individual number block:
(
\d{3} # any 3 numbers
(?:[ ]+|-)? # any (optional) spaces or a hyphen
\d{3} # any 3 numbers
(?:[ ]+|-)? # any (optional) spaces or a hyphen
\d{3} # any 3 numbers
)
Simplified:
if (preg_match('/^(\d{3}(?:[ ]+|-)?\d{3}(?:[ ]+|-)?\d{3})$/', $string)) {
If you want to restrict the separators to be only a single space or a hyphen, you can update the regex to use [ -] instead of (?:[ ]+|-); if you want this to be "optional" (i.e. there can be no separator between number groups), add in a ? to the end of each.
if (preg_match('/^(\d{3}[ -]\d{3}[ -]\d{3})$/', $string)) {
may it help you out.
Validator::extend('price', function ($attribute, $value, $args) {
return preg_match('/^\d{0,8}(\.\d{1,2})?$/', $value);
});

regexp replace between some slashes

I need help with write regex pattern for replace my variable.
Input data
strings for parse:
/hello/world/1111/2/3/4
/hello/world/2222/2/3/4
/hello/world/3333/2/3/4
/hello/world/3333
/hello/world/1111
replace value = some
Out
/hello/world/some/2/3/4
/hello/world/some/2/3/4
/hello/world/some/2/3/4
/hello/world/some
/hello/world/some
$out = preg_replace("#^/(\w+)/(\w+)/\d{4}(/.*)?$#", "/$1/$2/some$3", $in);
This will do the correct replacement for you, here a short explanation:
^ start of line
(\w+) >1 literals
\d{4} exactly 4 digits
(/.*)? anything starting with / or nothing at all
$ end of line
If they all have four digits in the part you want to replace, just do this:
$input = preg_replace("/\d{4}/", "some", $input);
If the input number is always four digits, then
$input = preg_replace("~/\d{4}(/.*)?$~", "some$1", $input);

php if preg_match 4 numbers then a dot and then 2 numbers

I am trying to write an if statement in php with preg_match to say allow 4 numbers and then a dot and then 2 numbers...
This is what I have....
$string = "10000.000";
if (preg_match('[/^\d{0,4}(\.\d{1,2})?$/]', $string)){
return TRUE;
} else {
return FALSE;
}
is my preg_match code wrong?
This should work as you want it!
^[0-9]{4}\.[0-9]{2}$
^ start of string,
[0-9] a number from 0 to 9,
{4} 4x times,
\. a dot,
$ end of the string

Categories