用PHP剪掉大句子

时间:2012-03-02 16:47:19

标签: php string

$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.';

如何将此字符串剪切为指定的字数?例如,5或10。

删除"&*$%>等字符。一些in-php功能?

也适用于非英语语言。

6 个答案:

答案 0 :(得分:3)

试试这个:

// 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);

答案 1 :(得分:2)

你可以尝试这样的事情。没有经过测试,可以使用一些修补,但它会给你一个想法。

$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));

答案 2 :(得分:1)

要删除所述字符,您可以按照以下方式执行操作:

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

如果要删除更多内容,只需向数组添加更多项目。

要剪切字符串,请执行此操作。请注意,如果您使用诸如supercalifragilisticexpialidocious之类的词语,您可能会获得长输出:

$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.

答案 3 :(得分:1)

使用此功能可以按字数拆分字符串:

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

答案 4 :(得分: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);

答案 5 :(得分: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 );

希望这会有所帮助:)

编辑:

抱歉误读了这个问题。我在想切割字母:(

你可以尝试

//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);

希望这有助于:)..