Can anyone help me with regular expression in php please .
I want print the first word in the first line and in the second line first word like the text document below
4 newland Avenue. Invoice 0004
London
W3 1lg
Result needs to be something like
"Address" => "4 newland Avenue London W3 1lg",
"Invoice" => "0004"
etc...
I need an expression to escape the invoice number since its in the same line of the first line of the address
I really appreciate your help and thanks in advance
To retrieve the invoice number only, assuming they are always 4 digits and the document is formatted just as you have shown... you can use the following regex.
/\b[Ii]nvoice (\d{4})\b/
In PHP you will likely use preg_match to get the invoice number.
Here is an example: Notice that only the digit is in the capture group and that this regex will ONLY match 4 digits that are preceded by the words "invoice" or "Invoice."
Related
:)
We would like to set a special condition (based on PHP Preg_match regular expression) to validates a number on our form.
That “number field” need, at first, only contain a max of 13 numbers (and only numbers. No letters or anything else).
The very first number need to be (only) “1” or “2” (not anything else)
The 4rd and 5rd number represent (the 2 numbers combinated) the “Month of birth” of someone, so the 4rd number need to be "0" or "1", and the 5rd need to be between "1" and "9".
Really appreciates if you can help us for that, to have the good “syntax” for the regular expression in PHP Preg_match to validates that field on our form! :)
Thanks to the community for your support and help!
Regards
Here is the literal regex pattern you have described to us:
^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$
Sample script:
$input = "1231212345678";
if (preg_match("/^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$/", $input)) {
echo "MATCH";
}
This regex pattern says to:
^ from the start of the string
[12] match 1 or 2 as the first digit
\d{2} then match any digits in the 2nd and 3rd position
(?:0[1-9]|1[0-2]) match 01, 02, ..., 12 as the two digit month
\d{8} then match any other 8 digits
$ end of string
I want to make the middle part of my number string bold.
I have a number string :
$nmbr="55113741659856";
I want to highlight 4 numbers in the middle ,from the 6th position
......7416......
and replace them with bold letters
<b>7416</b>
My currunt code is failing to do what I want
$nmbr="55113741659856";
preg_replace("/d+([0-9]{4,6})/i","<b>$1</b>",$nmbr);
Your Help is much appriciated.
Thanks.
I want to highlight 4 numbers in the middle ,from the 6th position
I'd do:
$nmbr="55113741659856";
preg_replace("/^(\d{5})(\d{4})/","$1<b>$2</b>",$nmbr);
You forgot to add \d+.
preg_replace("/\d+([0-9]{4,6})/i","<b>$1</b>",$nmbr);
The reason is:
\d Find a digit
And you missed the \ here.
Is there a function or a easy way to strip down phone numbers to a specific format?
Input can be a number (mobile, different country codes)
maybe
+4917112345678
+49171/12345678
0049171 12345678
or maybe from another country
004312345678
+44...
Im doing a
$mobile_new = preg_replace("/[^0-9]/","",$mobile);
to kill everything else than a number, because i need it in the format 49171 (without + or 00 at the beginning), but i need to handle if a 00 is inserted first or maybe someone uses +49(0)171 or or inputs a 0171 (needs to be 49171.
so the first numbers ALWAYS need to be countryside without +/00 and without any (0) between.
can someone give me an advice on how to solve this?
You can use
(?:^(?:00|\+|\+\d{2}))|\/|\s|\(\d\)
to match most of your cases and simply replace them with nothing. For example:
$mobile = "+4917112345678";
$mobile_new = preg_replace("/(?:^(?:00|\+|\+\d{2}))|\/|\s|\(\d\)/","",$mobile);
echo $mobile_new;
//output: 4917112345678
regex101 Demo
Explanation:
I'm making use of OR here, matching each of your cases one by one:
(?:^(?:00|\+|\+\d{2})) matches 00, + or + followed by two numbers at the beginning of your string
\/ matches a / anywhere in the string
\s matches a whitspace anywhere in the string (it matches the newline in the regex101 demo, but I suppose you match each number on its own)
\(\d\) matches a number enclosed in brackets anywhere in the string
The only case not covered by this regex is the input format 01712345678, as you can only take a guess what the country specific prefix can be. If you want it to be 49 by default, then simply replace each input starting with a single 0 with the 49:
$mobile = "01712345678";
$mobile_new = preg_replace("/^0/","49",$mobile);
echo $mobile_new;
//output: 491712345678
This pattern (49)\(?([0-9]{3})[\)\s\/]?([0-9]{8}) will split number in three groups:
49 - country code
3 digits - area code
8 digits - number
After match you can construct clean number just concatnating them by \1\2\3.
Demo: https://regex101.com/r/tE5iY3/1
If this not suits you then please explain more precisely what you want with test input and expected output.
I recommend taking a look at LibPhoneNumber by Google and its port for PHP.
It has support for many formats and countries and is well-maintained. Better not to figure this out yourself.
https://github.com/giggsey/libphonenumber-for-php
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$usNumberProto = $phoneUtil->parse("+1 650 253 0000", "US");
I have a export file that contains data of fixed widths:
Name (6)
Gender (6)
Phone Number (12) - Includes a space
Data.txt
DanielMale (07654) 521254
Lisa Female(16545) 654456
Sarah Female(54656) 4896546
I need to extract the name and gender data including any spaces if the data doesn't fit the data width.
The brackets for the phone number need to be ignored. (How do you ignore items in regular expressions?
I currently have the following regular expression, that pulls out the people's names. I thought I could simply add the bit on the white to make it pull out the Genders, but this doesn't work. Where am I going wrong?
/(?<name>.{6}+) (?<gender>.{6}+)/
I need the data to look like this at the end.
^ = space
Daniel
Male^^
07654 521254
Lisa^^
Female
16545 654456
Sarah^
Female
54656 4896546
This should catch all four fields:
/^(?<name>.{6})(?<gender>.{6})\((?<prefix>[^\)]+)\)\ (?<number>.+)/
The first ^ means match from the start.
{6}: match 6 times the pattern (the plus sign is redundant here)
\( and \): match brackets (without escaping they would mean boundaries of a subpattern)
[^\)]+ means "everything before the first closing bracket
I'm having some trouble with a regular expression for phone numbers. I am trying to create a regex that is as broad as possible for european phone numbers. The phone number can start with a + or with two leading 0's, followed by a number in between 0 and 40. this is not necessary however, so this first part can also ignored. After that, it should all be numbers, grouped into pairs of at least two, with a whitespace or a - inbetween the groups.
The regex I have put together can be found below.
/((\+|00)+[0-4]+[0-9]+)?([ -]?[0-9]{2,15}){1,5}/
This should match the following structures
0031 34-56-78
0032123456789
0033 123 456 789
0034-123-456-789
+35 34-56-78
+36123456789
+37 123 456 789
+38-123-456-789
...
What it also matches according to my javascript
+32 a54b 67-0:
So I must have made a mistake somewhere, but I really can't see it. Any help would be appreciated.
The problem is that you don't use anchors ^ $ to define the start and ending of the string and will therefore find a match anywhere in the string.
/^((\+|00)+[0-4]+[0-9]+)?([ -]?[0-9]{2,15}){1,5}$/
Adding anchors will do the trick. More about these meta characters can be found here.
Try this, may be can help you.
if (ereg("^((\([0-9]{3}\) ?)|([0-9]{3}-))?[0-9]{3}-[0-9]{4}$",$var))
{
$valid = true;
}
Put ^ in the beginning of the RegExp and $ in the end.