我如何replacePHP中的一部分string?
我想获得一个string的前10个字符,并想用'_'replace空格。 
我有
  $text = substr($text, 0, 10); $text = strtolower($text); 
但我不知道下一步该怎么做。
我想要的string
这是对string的testing。
成为
this_is_th
只需使用str_replace :
 $text = str_replace(' ', '_', $text); 
 在你之前的substr和strtolower调用之后,你会这样做: 
 $text = substr($text,0,10); $text = strtolower($text); $text = str_replace(' ', '_', $text); 
如果你想要看起来,但是,你可以在一行中做到这一点:
 $text = strtolower(str_replace(' ', '_', substr($text, 0, 10))); 
你可以试试
 $string = "this is the test for string." ; $string = str_replace(' ', '_', $string); $string = substr($string,0,10); var_dump($string); 
产量
 this_is_th 
这可能是你所需要的:
 $text=str_replace(' ', '_', substr($text,0,10)); 
做就是了:
 $text = str_replace(' ','_',$text) 
你需要首先剪下你想要的多less部分的string。 然后replace你想要的部分:
  $text = 'this is the test for string.'; $text = substr($text, 0, 10); echo $text = str_replace(" ", "_", $text); 
这将输出:
this_is_th