A regular expression to extract a business extension from a phone number - php

I am having the dang-est time trying to write a regex that will extract the phone extension from a full phone number string. This should work on a number like this one: 777.777.7777 x 7302
It should also work using "ext", "EXT", "Ext", "eXt", "ext.", and "Ext ". Essentially just cover all the common ground use of it.
I just need the "x 7302" part. In fact I am just going to strip it down to just the extension number once I extract it.
Can anyone help me please? Regular expressions are something that I struggle with when they get more complex.
I am doing this in a PHP function (preg_match) if that will help anyone.

This probably helps to give you something to play with:
$phonenumber = '777.777.7777 x 7302';
$extension = preg_replace('(^.*(ext|x) ?([0-9]+)$)i', '$2', $phonenumber);
echo $extension;
Use the i modifier (at the end) to make the regex case insensitive so to match all combinations of ext. I used a group to offer both variant: ext or x: (ext|x).
The rest is looking for a number at the end, and a space is possible between EXT and the number.

try with regex:
/e?xt?\.?\s*\d+$/i
<?
echo "<pre>";
$phone = '777.777.7777 x 7302';
preg_match("/e?xt?\.?\s*\d+$/i", $phone, $matched);
print_r($matched);
?>
Output:
Array
(
[0] => x 7302
)

\w+\s*\d+$
It is an simpler regex assuming that the input is similar to what you have provided.

This should do it for you
/([\d\-\.]+)([ext\. ]+)(\d+)/i
The first set matches the numbers separated by dash or dot. The second set matches your extension string and the third set matches your extension number.

If you just want the last numbers of the string, you can use:
\D+(\d+)$
\D+ at least one non-digit followed by:
(\d+) at least one digit (captured using parenthesis)
$ at the end of the string.

I am a bit leery of regular expression, so I'm a bit biased when I say, is it possible to just split the string using the PHP function "explode()", using the 'x' character as your delimiter?
Here is a link to the PHP manual for that function if you are not familiar with it:
Explode()

Try this function:
$pn = "777.777.7777 x 7302";
function get_ext($pn)
{
$ext = preg_replace('/[\d\.\s]+[ext\s]+(\d{1,})/i', '$1', $pn);
return $ext;
}
echo get_ext($pn);
//7302

Related

PHP Regex - how to remove string after "Newline * regards"

I've got strings like the following:
Hi X
Blah
Kind regards
ABC
And
Hi X
Blah
Regards
CBA
So the key is the newline and the word "regards" (case insensitive). I'd like to use PHP to get the part of the string before the line that contains "regards". E.g. for these examples, the result should just be:
Hi X
Blah
I've tried the below but it doesn't work as intended in some cases (E.g. if "Kind" appears multiple times in the string). Thanks in advance!
$matches = array();
if (preg_match("/\n(.*?)regards/i", $message, $matches) == 1) {
$stop_at = $matches[1];
$split = explode($stop_at,$message);
$message = $split[0];
}
What you're really after is a regex that handles multi-line strings. For this, you can use the m flag (PCRE_MULTILINE).
I would use preg_split() to split the string on your token, for example
$found = trim(preg_split('/^.*regards$/im', $message, 2)[0]);
Demo ~ https://3v4l.org/idMcP
Some notes:
I've used trim() to remove the empty line after "Blah" (your examples exclude it)
I've set a limit of 2 on preg_split(). This is redundant given you're only retrieving the first split but in my head, it means PHP does less work (realities may vary).
This might fail if a line ends in a word ending in "regards" but not necessarily the word "regards", for example this word I just made up "goregards" (it's like a shin guard but for viscera).
You can use the regular expression
(?si).+\b(?=\n+[\w ]*regards)
It will match everything up to a word boundary, then lookahead for newline(s) followed by a line which has regards on it (possibly preceeded by a combination of word characters or spaces).
$str = "Hi X
Blah
Kind regards
ABC";
preg_match('/(?si).+\b(?=\n\s*[\w ]*regards)/', $str, $match);

How to get a number from a html source page?

I'm trying to retrieve the followed by count on my instagram page. I can't seem to get the Regex right and would very much appreciate some help.
Here's what I'm looking for:
y":{"count":
That's the beginning of the string, and I want the 4 numbers after that.
$string = preg_replace("{y"\"count":([0-9]+)\}","",$code);
Someone suggested this ^ but I can't get the formatting right...
You haven't posted your strings so it is a guess to what the regex should be... so I'll answer on why your codes fail.
preg_replace('"followed_by":{"count":\d')
This is very far from the correct preg_replace usage. You need to give it the replacement string and the string to search on. See http://php.net/manual/en/function.preg-replace.php
Your second usage:
$string = preg_replace(/^y":{"count[0-9]/","",$code);
Is closer but preg_replace is global so this is searching your whole file (or it would if not for the anchor) and will replace the found value with nothing. What your really want (I think) is to use preg_match.
$string = preg_match('/y":\{"count(\d{4})/"', $code, $match);
$counted = $match[1];
This presumes your regex was kind of correct already.
Per your update:
Demo: https://regex101.com/r/aR2iU2/1
$code = 'y":{"count:1234';
$string = preg_match('/y":\{"count:(\d{4})/', $code, $match);
$counted = $match[1];
echo $counted;
PHP Demo: https://eval.in/489436
I removed the ^ which requires the regex starts at the start of your string, escaped the { and made the\d be 4 characters long. The () is a capture group and stores whatever is found inside of it, in this case the 4 numbers.
Also if this isn't just for learning you should be prepared for this to stop working at some point as the service provider may change the format. The API is a safer route to go.
This regexp should capture value you're looking for in the first group:
\{"count":([0-9]+)\}
Use it with preg_match_all function to easily capture what you want into array (you're using preg_replace which isn't for retrieving data but for... well replacing it).
Your regexp isn't working because you didn't escaped curly brackets. And also you didn't put count quantifier (plus sign in my example) so it would only capture first digit anyway.

how to get the text enclosed by quotations using php

So I have a pretty large dump file that I have to extract specific content from it.
The file has record each containing specific numbers enclosed by ". Bellow is a sample part of the file:
Ali Rabi (CustomerId=["3453456"]) // need to get: 3453456
Mohammad Reza Saberi (CustomerId=["12328"]) // need to get: 12328
Currently I read line by line and get the IDs as bellow. the code works fine and I get the result I want:
$cid = substr($row, strpos($row, '[') +2, strpos($row, ']')-strpos($row, '[')-2);
echo $cid;
But doesn't PHP have a function for this? getting the string enclosed by some delimiters?
If all your records look like the ones you've mentioned, I think it's the perfect place where you could use regular expressions.
Regular Expressions help you to create and find patterns in a given String.
For your case, you could probably use :
if (preg_match("/[0-9]+/", "Ali Rabi (CustomerId=[\"3453456\"])", $matches)) {
echo "Match was found <br />";
echo $matches[0];
}
The preg_match() function helps you to find the matches. The first param for this function is the pattern you're looking for. In your case you're looking for a set of continuous digits each of which can range from 0-9.
So, for a single digit we use [0-9]. Adding a + after [0-9] means that there needs to be atleast one digit in the match. Hence, [0-9]+ as the regular expression.
Read more about regular expressions in php : http://webcheatsheet.com/php/regular_expressions.php
Try:
<?php preg_match_all("/([1-9]+)/",$yourtext, $result, PREG_PATTERN_ORDER); ?>
$result contains all Matches.
Further Infomartion: http://php.net/manual/de/function.preg-match-all.php
I think you can use str_replace to remove the "
$cid = str_replace('"','',$row);

Replace from one custom string to another custom string

How can I replace a string starting with 'a' and ending with 'z'?
basically I want to be able to do the same thing as str_replace but be indifferent to the values in between two strings in a 'haystack'.
Is there a built in function for this? If not, how would i go about efficiently making a function that accomplishes it?
That can be done with Regular Expression (RegEx for short).
Here is a simple example:
$string = 'coolAfrackZInLife';
$replacement = 'Stuff';
$result = preg_replace('/A.*Z/', $replacement, $string);
echo $result;
The above example will return coolStuffInLife
A little explanation on the givven RegEx /A.*Z/:
- The slashes indicate the beginning and end of the Regex;
- A and Z are the start and end characters between which you need to replace;
- . matches any single charecter
- * Zero or more of the given character (in our case - all of them)
- You can optionally want to use + instead of * which will match only if there is something in between
Take a look at Rubular.com for a simple way to test your RegExs. It also provides short RegEx reference
$string = "I really want to replace aFGHJKz with booo";
$new_string = preg_replace('/a[a-zA-z]+z/', 'boo', $string);
echo $new_string;
Be wary of the regex, are you wanting to find the first z or last z? Is it only letters that can be between? Alphanumeric? There are various scenarios you'd need to explain before I could expand on the regex.
use preg_replace so you can use regex patterns.

How to strip this part of my string?

$string = "Hot_Chicks_call_me_at_123456789";
How can I strip away so that I only have the numberst after the last letter in the string above?
Example, I need a way to check a string and remove everything in front of (the last UNDERSCORE FOLLOWED by the NUMBERS)
Any smart solutions for this?
Thanks
BTW, it's PHP!
Without using a regular expression
$string = "Hot_Chicks_call_me_at_123456789";
echo end( explode( '_', $string ) );
If it always ends in a number you can just match /(\d+)$/ with regex, is the formatting consistent? Is there anything between the numbers like dashes or spaces?
You can use preg_match for the regex part.
<?php
$subject = "abcdef_sdlfjk_kjdf_39843489328";
preg_match('/(\d+)$/', $subject, $matches);
if ( count( $matches ) > 1 ) {
echo $matches[1];
}
I only recommend this solution if speed isn't an issue, and if the formatting is completely consistent.
PHP's PCRE Regular Expression engine was built for this kind of task
$string = "Hot_Chicks_call_me_at_123456789";
$new_string = preg_replace('{^.*_(\d+)$}x','$1',$string);
//same thing, but with whitespace ignoring and comments turned on for explanations
$new_string = preg_replace('{
^.* #match any character at start of string
_ #up to the last underscore
(\d+) #followed by all digits repeating at least once
$ #up to the end of the string
}x','$1',$string);
echo $new_string . "\n";
To be a bit churlish, your stated specification would suggest the following algorithm:
def trailing_number(s):
results = list()
for char in reversed(s):
if char.isalpha(): break
if char.isdigit(): results.append(char)
return ''.join(reversed(results))
It returns only the digits from the end of the string up to the first letter it encounters.
Of course this example is in Python, since I don't know PHP nearly as well. However it should be easily translated as the concept is easy enough ... reverse the string (or iterate from the end towards the beginning) and accumulate digits until you find a letter and break (or fall out of the loop at the beginning of the string).
In C it would be more efficient to use something a bit like for(x=strlen(s);x>s;x--) to walk backwards through the string, saving a pointer to the most recently encountered digit until we break or drop out of the loop at the beginning of the string. Then return the pointer into the middle of the string where our most recent (leftmost) digit was found.

Categories