我如何从字符串中删除一个字母

问题描述 投票:-2回答:1

我有类似的例子

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

在我的成就中我想从字符串行中删除web,所以我的预期结果应该像wap,net和我尝试的其他

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

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

   var_dump($str);

但无法实现

非常感谢提前

php string character
1个回答
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));
© www.soinside.com 2019 - 2024. All rights reserved.