我试图获取字符串的前10个字符,并希望用空格替换空格'_'
.
我有
$text = substr($text, 0, 10);
$text = strtolower($text);
Run Code Online (Sandbox Code Playgroud)
但我不知道下一步该做什么.
我想要字符串
这是对字符串的测试.
成为
this_is_th
Jon*_*hop 139
只需使用str_replace:
$text = str_replace(' ', '_', $text);
Run Code Online (Sandbox Code Playgroud)
您可以在之前substr
和之后进行此操作strtolower
,如下所示:
$text = substr($text,0,10);
$text = strtolower($text);
$text = str_replace(' ', '_', $text);
Run Code Online (Sandbox Code Playgroud)
但是,如果你想获得幻想,你可以在一行中完成:
$text = strtolower(str_replace(' ', '_', substr($text, 0, 10)));
Run Code Online (Sandbox Code Playgroud)
你可以试试
$string = "this is the test for string." ;
$string = str_replace(' ', '_', $string);
$string = substr($string,0,10);
var_dump($string);
Run Code Online (Sandbox Code Playgroud)
产量
this_is_th
Run Code Online (Sandbox Code Playgroud)
这可能就是您所需要的:
$text = str_replace(' ', '_', substr($text, 0, 10));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
244123 次 |
最近记录: |