文字列の最初の10文字を取得しようとしていますが、スペースを。に置き換えたいと考えてい'_'
ます。
私は持っています
$text = substr($text, 0, 10);
$text = strtolower($text);
しかし、次に何をすべきかわかりません。
文字列が欲しい
これは文字列のテストです。
なる
this_is_th
文字列の最初の10文字を取得しようとしていますが、スペースを。に置き換えたいと考えてい'_'
ます。
私は持っています
$text = substr($text, 0, 10);
$text = strtolower($text);
しかし、次に何をすべきかわかりません。
文字列が欲しい
これは文字列のテストです。
なる
this_is_th
単にstr_replaceを使用してください:
$text = str_replace(' ', '_', $text);
次のように、前回substr
のstrtolower
呼び出しの後にこれを行います。
$text = substr($text,0,10);
$text = strtolower($text);
$text = str_replace(' ', '_', $text);
ただし、派手になりたい場合は、次の1行で実行できます。
$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)
あなたは最初にあなたが望む数の部分にひもを切る必要があります。次に、必要な部品を交換します。
$text = 'this is the test for string.';
$text = substr($text, 0, 10);
echo $text = str_replace(" ", "_", $text);
これは出力します:
this_is_th