2012-03-02 5 views
0

この文字列を指定された単語数にするにはどうすればよいですか?例えば5または10である。PHPで大きな文章を切り取る

"&*$%>などの文字を削除します。いくつかのPHP関数ですか?

英語以外の言語にも対応する必要があります。

+2

[文字列から決められた数の単語だけを取得する方法php?](http://stackoverflow.com/questions/1112946/how-do-i-get-only-a-determined-number-of-words-from-a-string-in-php) –

+0

あなたは**この**文字列または**任意の**文字列をカットしたいですか? –

+0

'substr'を使用する必要があります – Bot

答えて

3

これを試してください:あなたはより多くのものを削除したい場合だけ、アレイに複数の項目を追加し

$patterns = array(); 
$patterns[0] = '/&/'; 
$patterns[1] = '/%/'; 
$patterns[2] = '/>/'; 
preg_replace($patterns, '', $string); 

// The number of words you want to keep 
$numwords = 5; 

// The symbols you want to have removed 
$stripChars = array('"', '&', '*', '$', '%', '>'); 

$string = '"Above all," said British Prime Minister David Cameron, "what I think matters is building the evidence and the picture so we hold this criminal regime to account, and to make sure it is held to account for crimes that it is committing against its people." He spoke to reporters outside a meeting of leaders of the European Union in Brussels, Belgium.'; 

$string = str_replace($stripChars, '', $string); 

$stringImpl = array_slice(explode(' ', $string, $numwords + 1), 0, $numwords); 
$stringCleaned = implode(' ', $stringImpl); 
+0

+1は私の考えと非常によく似ていて、早くそれを得ました。 –

1

は、あなたがこのの線に沿って何かを行うことができた文字を削除するには。

文字列をカットするにはこれを行います。

$newlen = 5; // change accordingly. 
$stringarray = explode(' ', $string); // Explodes the string into an array. One item for each row. 
$string = implode(' ', array_slice($stringarray, 0, $newlen)); // We then 'slice' the array, which basically cuts it. The 0 defines the starting point and the $newlen the end. After this we 'implode' it which basically converts it to a string. The ' ' shows what we want to stick in-between the items in the array. 
+0

これは最初の5文字を​​返しますが、単語は返しません。 – Chris

+0

申し訳ありませんが、質問を誤解しました。一定。 – Jack

2

あなたはこのような何かを試すことができます:あなたは、このようなsupercalifragilisticexpialidociousなどの単語を使用している場合、長い出力を得ることができます注意してください。テストされていないし、いくつかの微妙な変更を使用することができますが、それはあなたにアイデアを与える。

$num_words = 5; 
$string = '"Above all," said British Prime Minister David Cameron, "what I think matters is building the evidence and the picture so we hold this criminal regime to account, and to make sure it is held to account for crimes that it is committing against its people." He spoke to reporters outside a meeting of leaders of the European Union in Brussels, Belgium.'; 
$string = preg_replace('/["&*$%>]/i', '', $string); 
$words = explode(" ", $string); 
$newstring = implode(" ", array_slice($words, 0, $num_words)); 
1

この機能を使用して、単語の数で文字列を分割します

function substrwords($str, $n) { 
    $words = explode(' ',$str); 
    $outstr = ''; 
    for($i=0;$i<$n;$i++){ 
     $outstr .= $words[$i].' '; 
    } 
    return ltrim($outstr); 
} 
0

は、あなたが使用する必要があるのsubstr

$string = '"Above all," said British Prime Minister David Cameron, "what I think matters is building the evidence and the picture so we hold this criminal regime to account, and to make sure it is held to account for crimes that it is committing against its people." He spoke to reporters outside a meeting of leaders of the European Union in Brussels, Belgium.'; 

//specify the number after which the string should be cut 
$string_cut_position = 5; 

$new_string = substr($string, 0, $string_cut_position); 

のような特殊文字を削除するには:"&*$%>

$new_string = preg_replace('/["&*$%>]/i', '', $new_string); 

あなたはすべての英数字以外の文字を削除したい場合は、あなたが

$new_string = preg_replace("/[^a-zA-Z0-9\s]/", "", $new_string); 

希望を使用することができ、このことができます:)

EDIT:

申し訳ありませんが、質問を読み違えています。私は手紙をカットを考えていた:(

あなたはこのことができます:)

//specify the number after which the string should be cut 
$words_cut_position = 5; 

$new_string = array_slice(explode(' ', $string, $words_cut_position + 1), 0, $words_cut_position); 
$output_string = implode(' ', $new_string); 

希望を試すことができます。..

1

を何がしたいことは言葉あなたの希望数の間<br/>タグすなわち追加する場合にはここでは関数を使用することができます(しかし、関数名には満足していません)

function join_string($str, $word_count=5, $delimiter='<br/>') { 
    $words = preg_split('/\s/',preg_replace('/["&*$%>]/','',$str)); 
    // splits each word 
    $str = ''; 
    foreach ($words as $key => $value) { 
     $i = $key % $word_count; 
     if ($key > 0 && !$i) $str .= $delimiter; 
     // adds the delimiter 
     $str .= $value . ($i < $word_count-1 ? ' ' : ''); 
     // adds the space after the word 
    } 
    return $str; 
} 

echo join_string($string,5); 
関連する問題