2012-02-10 4 views
4

特定の数または単語の後に別のものの中にテキスト文字列を挿入したいと思います。たとえば:どのように単語の量の後にテキストを挿入する

$text_to_add = "#Some Text to Add#"; 
$text_original = "This is the complete text in which I want to insert the other text"; 
$number_words = 7; // Variable that will define after how many words I must put $text_to_add 

私は$ text_originalを印刷するとき、次の結果を取得したい:

これは#Someテキストは、#を追加するために、私は他のテキスト

を挿入する完全なテキストです

この関数http://php.net/manual/en/function.str-word-count.phpを使用して、単語の配列を取得し、$ text_to_addを挿入した新しい文字列を作成します。ただし、$ text_originalのテキストの一部が次のようになっているので、これを行うより良い方法があるかどうかは疑問です。非常に長い。

ありがとうございます。

答えて

3

これを試してみてください:

$arr = explode(" ",$text,$number_words+1); 
$last = array_pop($arr); 
return implode(" ",$arr)." ".$text_to_add." ".$last; 
+1

おかげで、完璧に動作します。 – leticia

関連する問題