regex: show all phone numbers - php

i want to see all phone numbers in my string. right now it has only one number in the array 'match' how can i get all the numbers in my array?
$str = "djvdsfhis 0647382938 rdfrgfdg tel:0647382938 rfgdfgfd 06 47 38 29
38 fdgdfrggfd tel:06-47382938 cxgvfdgsfdc";
$arr = '~\d{2}-\d{8}|\d{10}~';
$success = preg_match($arr, $str, $match);
if ($success) {
echo "Match: ".$match[0]."<br />";
print_r($match);
}
i get this as output:
djvdsfhis ffgfg 0647382938 rdfrgfdg tel:0647382938 rfgdfgfd 06 47 38 29 38 fdgdfrggfd tel:06-47382938 cxgvfdgsfdc
Match: 0647382938
Array ( [0] => 0647382938 )
but i want to have my array like this:
Array ( [0] => 0647382938 [1] => 0647382938 [2] => 06-47382938

You should use preg_match_all. Which will output an array of all of the results of your regex, in this instance an array of the numbers.
$str = "djvdsfhis 0647382938 rdfrgfdg tel:0647382938 rfgdfgfd 06 47 38 29
38 fdgdfrggfd tel:06-47382938 cxgvfdgsfdc";
$arr = '~\d{2}-\d{8}|\d{10}~';
$success = preg_match_all($arr, $str, $match);
if ($success) {
print_r($match);
}
Test it here :
http://sandbox.onlinephpfunctions.com/code/350d10b1be46ce3a5851d7671750bac28f9110f0

You can also use T-Regx tool which has automatic delimiters:
pattern('\d{2}-?\d{8}')->match($str)->all();

Related

Match hyphen character from a string to make an array output

I have a string that should contain a name and a date of registration:
Dipak Misra - 18 Nov 2018 10:20
I want to convert this string into array like this:
Array ( [0] => Dipak Misra [1] => 18 Nov 2018 [2] => 10:20 )
I am using preg_match_all() function:
$re = '/\w+(?:[-\s]\w+)*/';
$str = "Dipak Misra - 18 Nov 2018 10:20";
preg_match_all($re, $str, $matches);
print_r($matches[0]);
I am getting output Like this:
Array ( [0] => Dipak Misra [1] => 18 Nov 2018 10 [2] => 20 )
Please guide me.
If the format of that string does not change, you could split on either a space, hyphen space, or split on a space where on the left and the right side a digit is present instead of matching.
- |(?<=\d) (?=\d)
Regex demo
For example:
print_r(preg_split("/ - |(?<=\d) (?=\d)/", "Dipak Misra - 18 Nov 2018 10:20"));
Result:
Array
(
[0] => Dipak Misra
[1] => 18 Nov 2018
[2] => 10:20
)
Use preg_match() with ([^-]+)\s+-\s+(.*)\s(\d+:\d+) as pattern instead
$re = '/([^-]+)\s+-\s+(.*)\s(\d+:\d+)/';
$str = "Dipak Misra - 18 Nov 2018 10:20";
preg_match($re, $str, $matches);
unset($matches[0]);
print_r($matches);
Check result in demo
$pattern = "/(\w+\s\w+)\s-\s([0-9]{2}\s\w+\s[0-9]{4})\s([0-9]+:[0-9]+)/";
$chain = "Dipak Misra - 18 Nov 2018 10:20";
preg_match_all($pattern, $chain, $matches);
$data = [
'chain' => $chain,
'pattern' => $pattern,
'name' => $matches[1],
'date' => $matches[2],
'time' => $matches[3]
];
echo "<pre>";
print_r($data);
echo "<pre>";
the output

How to split a string in multiple ones (Php)?

I want to split a big number/string for example 123456789123456789 into 6 smaller strings/numbers of 3 characters each. So the result would be 123 456 789 123 456 789. How can I do this?
Use chunk_split():
$var = "123456789123456789";
$split_string = chunk_split($var, 3); // 3 is the length of each chunk
If you want your result as an array, you can use str_split():
$var = "123456789123456789";
$array = str_split($var, 3); // 3 is the length of each chunk
You may use chunk_split() function.
It splits a string into smaller
$string = "123456789123456789";
echo chunk_split ($string, 3, " ");
will output
123 456 789 123 456 789
First parameter is the string to be chunked. The second is the chunk length and the third is what you want at the end of each chunk.
See PHP manual for further information
You could do something like this:
$string = '123456789123456789';
preg_match_all('/(\d{3})/', $string, $matches);
print_r($matches[1]);
Output:
Array
(
[0] => 123
[1] => 456
[2] => 789
[3] => 123
[4] => 456
[5] => 789
)
\d is a number and {3} is 3 of the previously found character (in this case a number.
....
or if there won't always be even groupings:
$string = '12345678912345678922';
preg_match_all('/(\d{1,3})/', $string, $matches);
print_r($matches[1]);
Output:
Array
(
[0] => 123
[1] => 456
[2] => 789
[3] => 123
[4] => 456
[5] => 789
[6] => 22
)
Demo: https://regex101.com/r/rX0pJ1/1

preg_match_all pattern to get phone numbers

I have tried many many times to use preg_match_all for getting some phone numbers.
The things i want to get, with no problem is these structures :
09123456789
+989123456789
989123456789
0912 345 6789
+98 912 345 6789
How can i use preg_match_all to find the top numbers ?
they may have spaces or not.
All of them maybe start with +98 OR 98 For country code,
And then phone number Must start with 9 OR 0.
I have tried like this: (But it does NOT work for all)
/[+989][09]*([0-9]{9,})/i
I think you want something like this,
(?:\+?98|0)(?:\s*\d{3}){2}\s*\d{4}
DEMO
<?php
$str = <<<EOT
09123456789
+989123456789
989123456789
0912 345 6789
+98 912 345 6789
EOT;
$regex = '~(?:\+?98|0)(?:\s*\d{3}){2}\s*\d{4}~';
preg_match_all($regex, $str, $matches);
print_r($matches);
?>
Output:
Array
(
[0] => Array
(
[0] => 09123456789
[1] => +989123456789
[2] => 989123456789
[3] => 0912 345 6789
[4] => +98 912 345 6789
)
)
Try this:
(((\+?98)?|0) ?9[\d ]+)
See demo: http://regex101.com/r/rG6qE8/1

Split file up based on delimiters in php - which is the best to use?

I am trying to parse a file using php but I am not sure of the best way to do it. The file consists of stuff like:
saturn+5 57 space+shuttle 34 gemini 12 mercury 2 soyuz+tm 1
What I'm trying to do is split it up and populate a hash map, so..
$inventory["saturn+5"] = "57";
$inventory["space+shuttle"] = "34";
and so on.
I don't know how to tackle this.
I am trying to write a bit of regex to process the file to separate out the fields but I'm not having much luck and was wondering if I should try using a different approach using split() or explode().
Here's my approach using regular expression.
$data = 'saturn+5 57 space+shuttle 34 gemini 12 mercury 2 soyuz+tm 1';
$inventory = array();
preg_match_all('/(\S+) (\S+)/', $data, $matches);
foreach ($matches[1] as $index => $match) {
$inventory[$match] = $matches[2][$index];
}
print_r($inventory);
Output
Array
(
[saturn+5] => 57
[space+shuttle] => 34
[gemini] => 12
[mercury] => 2
[soyuz+tm] => 1
)
my crude approach:
<?php
echo '<pre>';
$str="saturn+5 57 space+shuttle 34 gemini 12 mercury 2 soyuz+tm 1";
//break it on space
$e=explode(' ',$str);
//reindex array to start from 1
array_unshift($e, "phoney");
unset($e[0]);
print_r($e);
$inventory=array();
foreach ($e as $k=>$v){
//detects odd key
if(($k+2)%2==1) {
$inventory[$v]= $e[$k+1];
}
}
print_r($inventory);
demo: http://codepad.viper-7.com/PN6K8m
output:
Array
(
[saturn+5] => 57
[space+shuttle] => 34
[gemini] => 12
[mercury] => 2
[soyuz+tm] => 1
)
It's actually quite trivial with a regex:
preg_match_all("/ ([\w+]+) \s (\d+) /x", $string, $m);
$assoc = array_combine($m[1], $m[2]);
You're just looking for a combination of alphanumeric characters \w and optional + signs, then a space, then a \d decimal.
array_combine will give you the associative array.
If it's always in that order, this will work:
<?
$foo = 'saturn+5 57 space+shuttle 34 gemini 12 mercury 2 soyuz+tm 1';
$foo_array = preg_split('/\s+/', $foo);
$hash = array();
for ($i = 0; $i < count($foo_array); $i++){
$i % 2 ? null : $hash[$foo_array[$i]] = $foo_array[++$i];
}
print_r($hash);
?>
Output:
php foo.php
Array
(
[saturn+5] => 57
[space+shuttle] => 34
[gemini] => 12
[mercury] => 2
[soyuz+tm] => 1
)

How to manipulate complex strings in php?

I am trying to group bunch of texts from a string and create an array for it.
The string is something like this:
<em>string</em> and the <em>test</em> here.
tableBegin rowNumber:2, columnNumber:2 11 22 33 44 tableEnd
<em>end</em> text here
I was hoping to get an array like the following results
array (0 => '<em>string</em> and the <em>test</em> here.',
1=>'rowNumber:5',
2=>'columnNumber:3',
3=>'11',
4=>'22',
5=>'33',
6=>'44'
7=>'<em>end</em> text here')
11,22,33,44 are the table cell data the user enters. I want to make them have unique index but keep the rest of texts together.
tableBegin and tableEnd are just the check for the table cell data
Any help or tips? Thanks a lot!
You may try the following, note that you need PHP 5.3+:
$string = '<em>string</em> and the <em>test</em> here.
tableBegin rowNumber:2, columnNumber:2 11 22 33 44 tableEnd
SOme other text
tableBegin rowNumber:3, columnNumber:3 11 22 33 44 55 tableEnd
<em>end</em> text here';
$array = array();
preg_replace_callback('#tableBegin\s*(.*?)\s*tableEnd\s*|.*?(?=tableBegin|$)#s', function($m)use(&$array){
if(isset($m[1])){ // If group 1 exists, which means if the table is matched
$array = array_merge($array, preg_split('#[\s,]+#s', $m[1])); // add the splitted string to the array
// split by one or more whitespace or comma --^
}else{// Else just add everything that's matched
if(!empty($m[0])){
$array[] = $m[0];
}
}
}, $string);
print_r($array);
Output
Array
(
[0] => string and the test here.
[1] => rowNumber:2
[2] => columnNumber:2
[3] => 11
[4] => 22
[5] => 33
[6] => 44
[7] => SOme other text
[8] => rowNumber:3
[9] => columnNumber:3
[10] => 11
[11] => 22
[12] => 33
[13] => 44
[14] => 55
[15] => end text here
)
Regex explanation
tableBegin : match tableBegin
\s* : match a whitespace zero or more times
(.*?) : match everything ungreedy and put it in group 1
\s* : match a whitespace zero or more times
tableEnd : match tableEnd
\s* : match a whitespace zero or more times
| : or
.*?(?=tableBegin|$) : match everything until tableBegin or end of line
The s modifier : make dots also match newlines
Here is the ugly way to do it, if you can't find a Regex guru out ther.
So, this is your text
$string = "<em>string</em> and the <em>test</em> here.
tableBegin rowNumber:2, columnNumber:2 11 22 33 44 tableEnd
<em>end</em> text here";
And this is my code
$E = explode(' ', $string);
$A = $E[0].$E[1].$E[2].$E[3].$E[4].$E[5];
$B = $E[17].$E[18].$E[19];
$All = [$A, $E[8],$E[9], $E[11], $E[12], $E[13], $E[14], $B];
print_r($All);
And this is the output
Array
(
[0] => stringandthetesthere.
[1] => rowNumber:2,
[2] => columnNumber:2
[3] => 11
[4] => 22
[5] => 33
[6] => 44
[7] => endtexthere
)
off-course, the <em> tags won't be visible, unless view the source code.

Categories