91

文字列の最初の10文字を取得しようとしていますが、スペースを。に置き換えたいと考えてい'_'ます。

私は持っています

  $text = substr($text, 0, 10);
  $text = strtolower($text);

しかし、次に何をすべきかわかりません。

文字列が欲しい

これは文字列のテストです。

なる

this_is_th

4

5 に答える 5

170

単にstr_replaceを使用してください:

$text = str_replace(' ', '_', $text);

次のように、前回substrstrtolower呼び出しの後にこれを行います。

$text = substr($text,0,10);
$text = strtolower($text);
$text = str_replace(' ', '_', $text);

ただし、派手になりたい場合は、次の1行で実行できます。

$text = strtolower(str_replace(' ', '_', substr($text, 0, 10)));
于 2012-09-26T15:21:52.233 に答える
8

あなたが試すことができます

$string = "this is the test for string." ;
$string = str_replace(' ', '_', $string);
$string = substr($string,0,10);

var_dump($string);

出力

this_is_th
于 2012-09-26T15:22:53.043 に答える
5

これはおそらくあなたが必要とするものです:

$text = str_replace(' ', '_', substr($text, 0, 10));
于 2012-09-26T15:23:15.553 に答える
4

ただ行う:

$text = str_replace(' ', '_', $text)
于 2012-09-26T15:22:31.030 に答える
2

あなたは最初にあなたが望む数の部分にひもを切る必要があります。次に、必要な部品を交換します。

 $text = 'this is the test for string.';
 $text = substr($text, 0, 10);
 echo $text = str_replace(" ", "_", $text);

これは出力します:

this_is_th

于 2015-09-23T04:55:47.943 に答える