需要提取字符串中的电话号码

时间:2017-01-04 04:23:38

标签: php regex preg-match

我有一个像下面这样的字符串,每次都可以改变。

  

style =“margin:0;”> \ r \ n
  电话\ r \ n< / p> \ r \ n< p style =“font-weight:bold; margin:   0;“> \ r \ n 0411313062 \ r \ n< / p> \ r \ n
  < / TD> \ r \ n
  < / TR> \ r \ n
  < TR> \ r \ n
  < td style =“padding-bottom:18px;”> \ r \ n

我需要从此字符串中提取电话号码0411313062。

3 个答案:

答案 0 :(得分:1)

试试这个

DispatchQueue.global(attributes: .qosDefault).async {

}

在行动中看到它,

https://regex101.com/r/pLqDWw/3

这将匹配7或10位数的电话号码,包括或不包含preg_match('\(?([0-9]{3})\s*\)?\s*-?\s*([0-9]{3})\s*-?\s*([0-9]{4})', $string, $match ); -,例如(area code)800-555-55558005555555或{{1等等

如果你必须匹配多个数字,我建议使用像PHPQuery这样的东西来优化你正在搜索它的文本(html)。你可以尝试一些更简单的东西,(800)555-5555但是一旦你掌握了PHPQuery,你就会感谢我。

答案 1 :(得分:0)

你可以试试这个:

$text = 'style="margin: 0;">\r\n
Phone\r\n </p>\r\n <p style="font-weight: bold; margin: 0;">\r\n 0411313062\r\n </p>\r\n
</td>\r\n
</tr>\r\n
<tr>\r\n
<td style="padding-bottom: 18px;">\r\n';
preg_match("/[0-9]{10}/", $text, $matches);// in case of multiple occurrences us preg_match_all
print_r($matches[0]);//0411313062

答案 2 :(得分:0)

此代码工作正常。试试这个

$tab = <<<EOD
style="margin: 0;">\r\n Phone\r\n </p>\r\n <p style="font-weight: bold;   margin: 0;">\r\n 0411313062\r\n </p>\r\n </td>\r\n
 </tr>\r\n
 <tr>\r\n
<td style="padding-bottom: 18px;">\r\n) EOD;


$input= explode("\r\n", $tab);
print_r($input);

电话号码

echo $input[4];