如何用空格分隔字符串中的电话号码使用php

时间:2015-08-10 06:26:47

标签: php regex

我有不同的字符串,包含这样的电话号码:

New order to car wash #663. Customer number is 7962555443. Thank you.

New order to car wash #663. Customer number is 50414. Thank you, bye.

New order to car wash #663. A phone number to connect with the customer is 905488739038.

我需要这个:

New order to car wash #663. Customer number is 7 9 6 2 5 5 5 4 4 3. Thank you.

New order to car wash #663. Customer number is 5 0 4 1 4. Thank you, bye.

New order to car wash #663. A phone number to connect with the customer is 9 0 5 4 8 8 7 3 9 0 3 8.

我需要分开包含3个以上符号的数字。

6 个答案:

答案 0 :(得分:2)

preg_replace单独没有任何回调函数就足够了。

preg_replace('~#\d+(*SKIP)(*F)|(?<=\d)(?=\d)~', ' ', $str);

DEMO

  • #\d+(*SKIP)(*F)匹配并丢弃所有以#开头的数字。
  • |
  • (?<=\d)(?=\d)现在,从剩余的字符串开始,这将匹配两个数字之间存在的边界。
  • 现在,用空格替换匹配的边界将为您提供所需的输出。

答案 1 :(得分:1)

您可以使用回调:

SELECT 
    case when Monday   ='Y' then'[Mon]' else '' end +case when Monday ='Y' then ',' else '' end + 
    case when Tuesday  ='Y' then'[Tue]' else '' end +case when Tuesday ='Y' then ','else '' end + 
    case when Wednesday='Y' then'[Wed]' else '' end +case when Wednesday ='Y' then ',' else '' end + 
    case when Thursday ='Y' then'[Thu]' else '' end +case when Thursday ='Y' then ',' else '' end + 
    case when Friday   ='Y' then'[Fri]' else '' end +case when Friday ='Y' then ',' else ''  end + 
    case when Saturday ='Y' then'[Sat]' else '' end +case when Saturday ='Y' then ',' else ''  end + 
    case when Sunday   ='Y' then'[Sun]' else '' end as classday
    FROM vw_Class_Without_Instructor

eval.in

答案 2 :(得分:1)

也可以使用\G anchor来完成此操作。替换为匹配的数字+空格:"$0 "

  

我需要分开包含3个以上符号的数字。

$str = preg_replace('~\b\d(?=\d{3})|\G\d\B~', "$0 ", $str);
  • \b\d匹配word-boundary \b后跟数字(\d [0-9] (?=\d{3})}
  • \d使用short在第一个|\G\d\B之后检查接下来的3个数字
  • \G或匹配\B 上一场比赛结束时的数字,然后是\h 非字边界

请参阅lookaheadregex101

上的测试

作为替代方案,也可以替换'use strict'; var app = angular.module('myApp', ['app.directives']); app.controller('AppCtrl', function($scope){ $scope.roles = [ {"id": 1, "name1": "Manager", "assignable": true}, {"id": 2, "name1": "Developer", "assignable": true}, {"id": 3, "name1": "Reporter", "assignable": true} ]; $scope.member = {roles: []}; $scope.selected_items = []; $scope.displayname = "name1"; }); var app_directives = angular.module('app.directives', []); app_directives.directive('dropdownMultiselect', function(){ return { restrict: 'E', scope:{ model: '=', options: '=', displayname: '=', pre_selected: '=preSelected' }, template: "<div class='btn-group' data-ng-class='{open: open}'>"+ "<button class='btn btn-small'>Select</button>"+ "<button class='btn btn-small dropdown-toggle' data-ng-click='open=!open;openDropdown()'><span class='caret'></span></button>"+ "<ul class='dropdown-menu' aria-labelledby='dropdownMenu'>" + "<li><a data-ng-click='selectAll()'><i class='icon-ok-sign'></i> Check All</a></li>" + "<li><a data-ng-click='deselectAll();'><i class='icon-remove-sign'></i> Uncheck All</a></li>" + "<li class='divider'></li>" + "<li data-ng-repeat='option in options'> <a data-ng-click='setSelectedItem()'>{{option[displayname ]}}<span data-ng-class='isChecked(option.id)'></span></a></li>" + "</ul>" + "</div>" , controller: function($scope){ console.log($scope.displayname ); $scope.openDropdown = function(){ $scope.selected_items = []; for(var i=0; i<$scope.pre_selected.length; i++){ $scope.selected_items.push($scope.pre_selected[i].id); } }; $scope.selectAll = function () { $scope.model = _.pluck($scope.options, 'id'); console.log($scope.model); }; $scope.deselectAll = function() { $scope.model=[]; console.log($scope.model); }; $scope.setSelectedItem = function(){ var id = this.option.id; if (_.contains($scope.model, id)) { $scope.model = _.without($scope.model, id); } else { $scope.model.push(id); } console.log($scope.model); return false; }; $scope.isChecked = function (id) { if (_.contains($scope.model, id)) { return 'icon-ok pull-right'; } return false; }; } } }); 水平空间后的第一个数字eval.in

答案 3 :(得分:0)

试试这个。

jQuery(statusIcon).attr('src',ICO_ERR);

答案 4 :(得分:0)

您可以使用implode首先使用str_split将字符串转换为数组:

$number="905488739038";    
$formatted = implode(' ',str_split($number));

echo $formatted;

输出:

9 0 5 4 8 8 7 3 9 0 3 8

参考:http://www.php.net/manual/en/function.str-split.php

答案 5 :(得分:0)

你也可以试试这个正则表达式:

((?:(?:\d)\s?){4,})

它将捕获长度为四或更长的所有数字。此外,您还需要执行一个额外步骤,从结果中删除匹配中的空格,如下所示:

7 9 6 2 5 5 5 4 4 3

Demo