doubianyan9749 2012-07-17 06:45
浏览 6
已采纳

如何在php中使用preg删除字符串的一部分?

I want to take out GP123 (a string consisting of digits and characters in any order) from a bigger string ($str) and exclude Hello ($str1) which will be a string in another variable. My code is this. But it is not working. How can I remove a part of a string.

$str=" HelloGP123";
$str1="Hello";
preg_match_all("/(?:$str1)\w+\d+\w+/",$str,$str2);
print_r($str2); 
  • 写回答

5条回答 默认 最新

  • douyi6168 2012-07-18 19:04
    关注

    Well My need was just to erase $str1(Hello) part from $str (HelloGP123) and store final remainder in $str2. And I was going all around Regular Expression.

    But I realized this can be solved in a much easier way by string replacement. So here is what I did and it worked.

    $str2= str_ireplace($str1, '', $str);
    

    I guess this is the most easiest method than storming our brain by trying various combinations of regex.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大