check telephone number goes wrong - php

i use this to check if there is a 06 number in the string. the 06 number is always 10 numbers.
$string = "This is Henk 0612345678";
$number = preg_replace('/[^0-9.]+/', '', $string);
echo $number;
This is working good, but when the string is
$string = "This is 12Henk 0612345678";
The number is 120612345678
I dont want the 12 into it, but 12 is not always the same in the string.
How can i check only for a 10 digits number ?
Thanks

This could help you
/([ \w]+)(06[0-9.+]{8})/
the 1st () is the entire String before the 06 and the 2nd() is the Number starting with 06 and 8 digits.
The solution does not cover the case where a 06 comes before the number sequence

Rather than replacing everything that's not what you want, try searching for what you do want with preg_match.
That makes it a lot easier to be specific:
The number always starts 06, so you can hard-code that in your regex
That's followed by exactly 8 more digits, which you can specify as [0-9]{8} or \d{8} ("d" for "digit")
To avoid matching longer numbers, you can surround that with \b for "word break"
Put it together, and you get:
preg_match('/\b06\d{8}\b/', $string, $matched_parts);
This doesn't change $string, but gives you an array in $matched_parts containing the matched parts; see the preg_match documentation for a detailed explanation.

Related

get the first number in string if there is one

I am looking for a way to extract the first number out of a string. This examples
43
432Phill 21
432hill 21
43#1 Example
43,123 example
should return
43
432
432
43
43,123
I assume it would be possible to use strpos and iterate needle from a-z, A-Z and #. to get all positions and than use the lowest non-zero one to determine the point where number ends. This seems like an overkill. Is there a better way of doing it ?
EDIT: the position is to use substr later. If there is a better way I am down with it.
With a regex, you can extract numbers from a string.
The following statement should do the trick:
preg_match_all('~\d+~', $string, $match);
$match is the array of the numbers contained in $string.
If the number starts at begining you can do this like this:
$number = floatval($string); //it will return float e.g. 432
If you use comma instead of decimal point e.g. 43,123 you must first replace it with dot:
$string = str_replace(',', '.', '43,123 example');

What pattern to use in preg_match to get value from string

I have two strings
First One:
Date: Sat, 13 Jun 2015 13:26:05 +0100
Subject: Changing the balance: +50,00 CZK
Dear client,
Second One:
Date: Sat, 14 Jun 2015 14:58:05 +0100
Subject: Changing the balance: +75,00 CZK
Dear client,
And I really don't know what pattern to use if I want to get the number of CZKs from these strings. I need integer 50 from first string and integer 75 from second string (just integer not decimal with ,00).
This can really be as simple or as complex as you need it to be. In it's simplest form, you could look for a pattern that reads:
number.comma.number.space.CZK
this can be written as:
[0-9]+,[0-9]+\sCZK
[0-9] is a range, between 0-9 (number). The plus character means that at least 1 number is required. If you wanted to make this EXACTLY 2 numbers you could change [0-9]+ for [0-9]{2}
, is a comma...
[0-9]+ is another number (at least 1)
\s is a space
CZK is the string you're wanting to end with
You can expand upon this as you wish. Here is a working example: http://regexr.com/3baog
Edit:
If you wish to capture the 50 / 75, you need to wrap parenthesis around the part you're after, eg:
([0-9]+),[0-9]+\sCZK
Use positive lookahead to select the integer part of number before CZK
\d+(?=,\d*\s*CZK)
Explanation for the above regex can be seen from this DEMO
If you want to select the sign + or -, then you can add [+-]? at the beginning.

Position in string of 4 digits number begining with "20"

I've got a string:
$string = "Something here 2014 another text here";
I need to detect position of the first 4 digits number that begins with "20".
So the result of the example would be 15th character of the $string.
Since you have commented with code you tried, I now feel comfortable answering your question properly :) Thank you for trying first!
Your attempt:
preg_match('/20\d\d/', "Something here 2014 another text here",
$matches, PREG_OFFSET_CAPTURE);
... is absolutely correct, however as you correctly pointed out, it would also match 20140 (and indeed 12014 would match too).
To fix this behaviour, you can add word boundaries - because numbers count as word characters. Your regex becomes:
'/\b20\d\d\b/'
This will ensure that there are no numbers (or letters, for that matter) immediately before or after your target four-digit number :)
What about...
$needle = "20";
$pos = strpos($string , $needle);
EDIT:
as requested, a way to get the string from this
$date = substr ($string , $pos , 4 ]);

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

php preg_replace frustration

Im reluctant to ask but I cant figure out php preg_replace and ignore certain bits of the sting.
$string = '2012042410000102';
$string needs to look like _0424_102
The showing numbers are variable always changing and 2012 changes ever year
what I've tried:
^\d{4}[^\d{4}]10000[^\d{3}]$
^\d{4}[^\d]{4}10000[^\d]{3}$
Any help would be appreciated. I know it's a noob question but easy points for whoever helps.
Thanks
Your first regex is looking for:
The start of the string
Four digits (the year)
Any single character that is not a digit nor { or }
The number 10000
Any single character that is not a digit nor { or }
The end of the string
Your second regex is looking for:
The start of the string
Four digits (the year)
Any four characters that are not digits
The number 10000
Any three characters that are not digits
The end of the string
The regex you're looking for is:
^\d{4}(\d{4})10000(\d{3})$
And the replacement should be:
_$1_$2
This regex looks for:
The start of the string
Four digits (the year)
Capture four digits (the month and day)
The number 10000
Capture three digits (the 102 at the end in your example)
The end of the string
Try the following:
^\d{4}|10000(?=\d{3}$)
This will match either the first four digits in a string, or the string '10000' if there are three digits after '10000' before the end of the string.
You would use it like this:
preg_replace('/^\d{4}|10000(?=\d{3}$)/', '_', $string);
http://codepad.org/itTgEGo4
Just use simple string functions:
$string = '2012042410000102';
$new = '_'.str_replace('10000', '_', substr($string, 4));
http://codepad.org/elRSlCIP
If they're always in the same character locations, regular expressions seem unnecessary. You could use substrings to get the parts you want, like
sprintf('_%s_%s', substr($string,4,4), substr($string,13))
or
'_' . substr($string,4,4) . '_' . substr($string,13)

Categories