我想改变一个字符串的值

I want to change the value of a string

我有一个值为 Grade 1.I 的字符串,我想将其更改为 grade_1

这是我的代码。

         $gradeselection="Grade 1";          //it is getting value from a select box .this is just for understanding.

我用过preg_replace但没有得到解决方案。

使用

strtolower(str_replace(" ","_","Grade 1"));

试试这个:

$gradeselection="Grade 1";

echo strtolower(str_replace(" ","_", $gradeselection));
// str_replace replace _ with space and than convert it into lower case

您可以使用 str_replace
而不是 preg_replace 试试这个

echo str_replace("Grade 1","grade_1",$gradeselection);