Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用php将空格替换为下划线_Php_Replace - Fatal编程技术网

用php将空格替换为下划线

用php将空格替换为下划线,php,replace,Php,Replace,这是一个非常新的问题,但我就是不知道问题出在哪里 所以请容忍我 这就是我想要实现的->$new='天哪,这是一个愚蠢的错误' 下面是我从这段代码中得到的->$new='天哪,这是一个愚蠢的错误' 使用以下功能: <?php $old = 'OMG_This_Is_A_One_Stupid_Error'; $new = str_replace(' ', '_', $old); echo $old; // will output OMG This Is A One St

这是一个非常新的问题,但我就是不知道问题出在哪里 所以请容忍我

这就是我想要实现的->$new='天哪,这是一个愚蠢的错误'

下面是我从这段代码中得到的->$new='天哪,这是一个愚蠢的错误'

使用以下功能:

<?php
    $old = 'OMG_This_Is_A_One_Stupid_Error';
    $new = str_replace(' ', '_', $old);
    echo $old; // will output OMG This Is A One Stupid error
?>
反转参数以获得反转效果

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

请允许我把你介绍给

你可以用

如果只希望源字符串中的下划线替换为空格,请使用以下命令:

$source = 'OMG This Is A One Stupid Error'; //just an example

// $new is: OMG_This_Is_A_One_Stupid_Error
$new = str_replace(' ', '_', $source);
如果源字符串是较大字符串的子字符串,则可以执行以下操作:

$source     = 'This is a question for SO. '
            . 'OMG This Is A One Stupid Error';
$to_replace = 'OMG This Is A One Stupid Error';
$target     = str_replace(' ', '_', $to_replace);

// Finally replace the target string
$new = str_replace($to_replace, $target, $source)

// $new is: This is a question for SO. OMG_This_Is_A_One_Stupid_Error

你可能想抬头看看,或者换言之:你不仅重新发明了轮子,还发明了火和用石头杀死动物作为食物;参数顺序错误:mixed str_replace mixed$search,mixed$replace,mixed$subject[,int&$count]
$source = 'OMG This Is A One Stupid Error'; //just an example

// $new is: OMG_This_Is_A_One_Stupid_Error
$new = str_replace(' ', '_', $source);
$source     = 'This is a question for SO. '
            . 'OMG This Is A One Stupid Error';
$to_replace = 'OMG This Is A One Stupid Error';
$target     = str_replace(' ', '_', $to_replace);

// Finally replace the target string
$new = str_replace($to_replace, $target, $source)

// $new is: This is a question for SO. OMG_This_Is_A_One_Stupid_Error