dtng25909 2011-12-17 03:12
浏览 18
已采纳

PHP正则表达式 - 删除字符后

I have PHP strings like

$str1 = "hello ... this is the rest of the line"

or

$str1 = "ASDFDF ... this is also the rest of the line";

I am trying to right a regex statement that will extract the text after "..." appears in the string. I am not able to do this reliably..

so in the above cases, i want to...

 $extract = "this is the rest of the line";

... you get the point.

  • 写回答

3条回答 默认 最新

  • dqqyp90576 2011-12-17 03:16
    关注

    There are multiple ways to do it.

    Using strpos and substr:

    function rest_of_line($line){
      $loc = strpos($line, '...');
      if($loc !== FALSE){
          return substr($line, $loc+3);
      }
      return $line;
    }
    
    $str1 = "hello ... this is the rest of the line";
    $str2 = "ASDFDF ... this is also the rest of the line";
    echo rest_of_line($str1);
    echo rest_of_line($str2);
    

    Or using explode:

    $rest = explode('...', $str1, 2); // the 2 ensures that only the first occurrence of ... actually matters.
    echo $rest[1]; // you should probably check whether there actually was a match or not
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染