用php替换带下划线的空格

Var*_*ija 0 php replace

这是一个非常新手的问题,但我无法弄清楚问题出在哪里,所以请耐心等待...

这是我想要实现的目标 - > $new = 'OMG_This_Is_A_One_Stupid_Error';

这是我从这段代码得到的 - >$new = 'OMG This Is A One Stupid_Error';

<?php 
    $find = 'OMG This Is A One Stupid Error'; //just an example
    $offset = 0;
    $search = ' ';
    $length = strlen($search);
    $replace = '_';
    while($substring = strpos($find, $search,$offset))
    {
        $new =  substr_replace($find, $replace,$substring,$length);
        $offset = $substring + $search_length;
    }   
    echo $new;
?> 
Run Code Online (Sandbox Code Playgroud)

Vla*_*mos 6

使用str_replace()函数:

<?php
    $old = 'OMG_This_Is_A_One_Stupid_Error';
    $new = str_replace(' ', '_', $old);
    echo $old; // will output OMG This Is A One Stupid error
?>
Run Code Online (Sandbox Code Playgroud)

反转参数以获得反向效果

<?php
    $old = 'OMG This Is A One Stupid_Error';
    $new = str_replace('_', ' ', $old);
    echo $old; // will output OMG_This_Is_A_One_Stupid error
?>
Run Code Online (Sandbox Code Playgroud)