Check if a String with a only Number and space in PHP - php

I try check a string is number or not.
number format:
2222 or 22 22 or 1 2222 or 22323423
(with space before,midlle and after digits)
Is better I use regular expression?
So how can I change it to do that?
preg_grep('~^[0-9]$~'
or Is there any faster method?

You can use this regex:
^\d+( +\d+)*$
This will allow 1 or more spaces only in the middle of the number but not at start or end.

Related

How to extract an n digits number in different forms ( separated by a space) - PHP

I need to extract an 8 digit number from different text files. The issue is that there may be or may not a space before and after each digit as below.
Each client places the space however it thinks it makes the number more readable, hence the issue extracting it.
33113306
33 11 33 06
3311 3306
There may be numbers with more than 8 digits. Those should be ignored. E.g.
33 11 33 06 //the number we need
28 232392 93293293923 // the number we don't need
There is always a space before the first digit.
The 8 digit number we need is placed between 'words' or other characters. However sometimes we have new words on new lines. E.g.
This is a number
Al : 33 11 33 06 ,
Another number we don't need 232 2323 232 2, ..sdsad
I'm using PHP with preg_match and my old regex fails due this new "feature" to allow the client to define the number in different forms.
^(\d){8}$
You can use PHP's filter_var function, to filter out everything except numbers, plus and minus. You then use str_replace to remove the minus and plus (in case there is any):
$string=str_replace("+","",str_replace("-","",filter_var($string, FILTER_SANITIZE_NUMBER_INT)));
if(strlen($string)!=8){
echo "Error";
} else {
echo "Success";
}
You could just remove non-numeric characters, and get the first 8 characters of the string:
echo substr(preg_replace("/[^0-9,.]/", "", $string), 0, 8);
With your current input, you can use this:
[ ]\K(?<!\d )(?=(?: ?\d){8})(?!(?: ?\d){9})\d[ \d]+\d
See what matches and doesn't match in the regex demo.
[^ \d]+(\d\s*){8}[^ \d]+
Try this

Get String Based on a Certain Length Without Cutting It

Lets say I need to get a string from MySQL database smaller than 150 characters BUT I do not want to cut the last word, instead I need it until the last space and less than 150 characters.
For example:
I want:
Derrick Rose and the Chicago Bulls.
I don't want:
Derrick Rose and the Chica.
Is there a way to do this in MySQL, PHP or a combination of both?
You can do this with the built-in string functions:
reverse the first 150 characters of the string
find the first space in the reversed string
use this information to get the right string
The SQL looks something like this:
select left(left(str, 150), 150 - locate(' ', reverse(left(str, 150))))
Write a loop in PHP that starts at position 150, and works back until it encounters either a space character, or the start of the string.
If it encounters a space character, take all characters from the start of the string to the position you just found. Otherwise, use the first 150 characters (edge case that there are no space characters in the first 150).

How to select phone number from a paragraph using regular exp

I want to select phone number from a paragraph.But the format of phone numbers are
123-456-7890
123.456.7890
123*456*7890
(202) 553-6381
123_456_7890
1234567890
1.2.3.4.5.6.7.8.9.0
1*2*3*4*5*6*7*8*9*0
954 665 53 92
456 7890 x123
I got a regular expression but it not work in all cases.The regular expression is
$pat1="/(\d)?(0-9)|(\s|-|_|.|)?(\()?(\d){3}(\))?(\s|-|_|.){1}(\d){3}(\s|-|_|.){1}(\d){4}/";
It may not be the best but it will match all instances
(\d{3}([\-\.\*\s_]?\d{3})([\-\.\*\s_]?\d{4}))|\d(([\.\*]\d){9})|(\(\d{3}\)\s\d{3}\-\d{4})|(\d{3}\s\d{3}\s\d{2}\s\d{2})|(\d{3}\s\d{4}\sx\d{3})
a simple uncompressed regex can be this:
^\d{3}\s\d{3}\s\d{2}\s\d{2}|\d{3}\s\d{4}\sx\d{3}|\d{10}|\(?\d{3}\)?[\s-.*_]\d{3}[-.*_]\d{4}|(\d[*.]){9}\d$
note that in last line there is x123, if it is a four digit number you must change regex to:
^\d{3}\s\d{3}\s\d{2}\s\d{2}|\d{3}\s\d{4}\s\d{4}|\d{10}|\(?\d{3}\)?[\s-.*_]\d{3}[-.*_]\d{4}|(\d[*.]){9}\d$
also, ^ means begins with, and $ means ends with. for some regex queries you must remove them.

check if 5 digit number is available in a string

$st1='dsdsdsd 97537 sdsdd dsddd';
$st2='fdsf 23e sdsd 434 432443454';
$st3='fdf97537 ds344dsddd';
I want to check whether a 5 digit number is available in a string.
st1-- has 5 digit number
st2--- not
st3-- has 5 digit number
A simple regex will do the job.
preg_match('/\d{5}/', $input)
See also http://www.php.net/preg_match
Try this regular expression with preg_match() or preg_match_all()
preg_match("/\b[^\d]*\d{5}[^\d]*\b/", $str);
Let's assume that each element to be checked if this is five digits number is separated by space in string. Therefore you may use explode function to convert string into array of substrings. next you can use is_numeric function to check if that is digit along with check if that sub string is five length long. Also you may use regular expression for that.
Here RegEx is far more better. As I see the #Matt's answer meets these requirements, my comments will be unnecessary.

i want check input by regex | contain 16 number

I have input, I want to check it.
It should accept only numbers, and must be 16, no more and no less.
How I can do that using php and regex?
Regex would be ^\d{16}$
preg_match("/^\\d{16}$/", $str);
^\d{16}$
Explication:
^ for the beginning of the line (so nothing before that)
\d meaning a number (decimbal)
{16} to mean "exactly 16"
$ for the ending of the line (so nothing after that)
I think you need it for the credit card validator
^(\d{4}[- ]){3}\d{4}|\d{16}$
this will match :
1234-1234-1234-1234 | 1234 1234 1234 1234 | 1234123412341234
Please note that this is copied from regexlib
Would it not be better to make sure its 16 characters and numbers only with is_numeric() and strlen().

Categories