【问题标题】:how do i remove a letter from string如何从字符串中删除一个字母
【发布时间】:2017-12-23 12:25:51
【问题描述】:

我有类似的字符串 例子

    $str = 'wap,net,web,andothers';

在我的成就中,我想从字符串行中删除 web,所以我的预期结果应该是 wap,net,andothers 我试试

     $str = 'wap,net,web,andother';

       $remove = 'web';  
     $str = ltrim($str, ' $remove');

   var_dump($str);

但做不到

提前致谢

【问题讨论】:

  • 变量没有在单引号内展开,你完全误解了ltrim() 的作用。

标签: php string character


【解决方案1】:

这里我爆一下然后过滤掉关键字web

<?php
$str = 'wap,net,web,andother';
$explode = explode(',',$str);
$result = [];

foreach($explode as $value){
    if($value !== 'web'){
        $result[] = $value;
    }
}

print_r(implode(',',$result));

?>

或者简单地说,使用 php str_replace

$str = 'wap,net,web,andother';
$stringToRemove = 'web';

print_r(str_replace($stringToRemove,'',$str));

【讨论】:

  • 确实不错,但我可以在 str_replace 中使用变量吗?就像例子 $str = 'wap,net,web'; $remove == '网络'; $st = str_replace('$remove','',$str); var_dump($st);
  • 非常明智的请为什么我投反对票以及我该如何关闭它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多