drutjkpsr67393592 2014-08-14 10:58
浏览 62

PHP - 从字符串中修剪文本,并从数组中删除空格

I have the following PHP code that is suppose to remove the 'excess' text when a user enters it in a field. I have the following code as follows:

$mobileNumber = '00 356 99048123';
$excessMobileNumbers = array(//remove excess items in mobile number such as dialing codes and empty spaces
   // ' ' => '',
    '00356' => '',
    '+356' => '',
    '356' => '',  
    '00' => '',
);

The output is 99048123

The above code works well as it the number 99048123 doesn't contain 356 or 00.

But when I use this number 00 356 99008123, the number 99008123 contains 00. I want it only to remove the 00 in the 00 i.e. starting from the left hand side and leaving without removing the 00 in the 99008123.

How do I go about it? I use the array as a 'filtering' system.

Thanks

Clarification It is not only for 00 even for 356, if the number is 99048123 it works fine. If this number 99035612 since it has 356 withing it it does not work.

SOLUTION I discovered this solution which seems to work for my problem.

$mobileNumber = '00 356 99048000';
$mobileNumber = str_replace(' ','',$mobileNumber); // UPDATE
$excessMobileNumbers = substr($mobileNumber, 0, -8);
$mobileNumber = str_replace($excessMobileNumbers,'',$mobileNumber);
echo $mobileNumber;

Thank you all for your contribution.

  • 写回答

5条回答 默认 最新

  • dtbl1231 2014-08-14 11:02
    关注

    As @ende-neu suggests simply explode out the string on a space and just take the last element in the array.

    For the example you give this would look like:

      $mobileNumber = '00 356 99048123';
      $mobile_array = explode(' ',$mobile_number);
      $my_number = end($mobile_array); //will give you 99048123
      echo $my_number;
    

    Although to be honest you're probably better off with a regular expression type approach or use a solid library such as libphonenumber for PHP

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效