使用 PHP 将字符串分成两半(单词感知)

问题描述 投票:0回答:9

我正在尝试将字符串分成两半,并且它不应该在单词中间拆分。

到目前为止,我想出了以下99%有效的方法:

$text = "The Quick : Brown Fox Jumped Over The Lazy / Dog";
$half = (int)ceil(count($words = str_word_count($text, 1)) / 2); 
$string1 = implode(' ', array_slice($words, 0, $half));
$string2 = implode(' ', array_slice($words, $half));

这确实有效,可以根据字符串中的单词数正确地将任何字符串分成两半。但是,它会删除字符串中的任何符号,例如对于上面的示例,它将输出:

The Quick Brown Fox Jumped
Over The Lazy Dog

我需要在分割后保留字符串中的所有符号,例如:和/。我不明白为什么当前的代码要删除符号...如果您可以提供替代方法或修复此方法以不删除符号,我们将不胜感激:)

php string split
9个回答
24
投票

在查看您的示例输出时,我注意到我们所有的示例都已关闭,如果字符串的中间位于单词内,我们会向 string1 提供较少的值,而不是提供更多的值。

例如

The Quick : Brown Fox Jumped Over The Lazy / Dog
的中间是
The Quick : Brown Fox Ju
,它位于单词的中间,第一个示例为 string2 提供了分割单词;下面的例子给出了 string1 的分割词。

在分割词上减少 string1

$text = "The Quick : Brown Fox Jumped Over The Lazy / Dog";

$middle = strrpos(substr($text, 0, floor(strlen($text) / 2)), ' ') + 1;

$string1 = substr($text, 0, $middle);  // "The Quick : Brown Fox "
$string2 = substr($text, $middle);  // "Jumped Over The Lazy / Dog"

在分割词上给予 string1 更多

$text = "The Quick : Brown Fox Jumped Over The Lazy / Dog";

$splitstring1 = substr($text, 0, floor(strlen($text) / 2));
$splitstring2 = substr($text, floor(strlen($text) / 2));

if (substr($splitstring1, 0, -1) != ' ' AND substr($splitstring2, 0, 1) != ' ')
{
    $middle = strlen($splitstring1) + strpos($splitstring2, ' ') + 1;
}
else
{
    $middle = strrpos(substr($text, 0, floor(strlen($text) / 2)), ' ') + 1;    
}

$string1 = substr($text, 0, $middle);  // "The Quick : Brown Fox Jumped "
$string2 = substr($text, $middle);  // "Over The Lazy / Dog"

8
投票
function split_half($string, $center = 0.4) {
        $length2 = strlen($string) * $center;
        $tmp = explode(' ', $string);
        $index = 0; 
        $result = Array('', '');
        foreach($tmp as $word) {
            if(!$index && strlen($result[0]) > $length2) $index++;
            $result[$index] .= $word.' ';
        }
        return $result;
}

演示:http://codepad.viper-7.com/I58gcI


4
投票

我知道这是一个老问题,但我有以下代码应该可以完成所需的操作。

默认情况下,它会在中间之后第一次出现空格时分割字符串。 如果中间后面没有空格,则会查找中间之前的最后一个空格。

function trim_text($input) {
    $middle = ceil(strlen($input) / 2);
    $middle_space = strpos($input, " ", $middle - 1);

    if ($middle_space === false) {
        //there is no space later in the string, so get the last sapce before the middle
        $first_half = substr($input, 0, $middle);
        $middle_space = strpos($first_half, " ");
    }

    if ($middle_space === false) {
        //the whole string is one long word, split the text exactly in the middle
        $first_half = substr($input, 0, $middle);
        $second_half = substr($input, $middle);
    }
    else {
        $first_half = substr($input, 0, $middle_space);
        $second_half = substr($input, $middle_space);
    }
        return array(trim($first_half), trim($second_half));
}

这些是示例:

示例1:

“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW”

拆分为

“WWWWWWWWWWWWWWWWWWWW”

“WWWWWWWWWWWWWWWWWWWW”

示例2:

“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW”

拆分为

“WWWWWWWWWWWWWWWWWWWW”

“WWWWWWWWWWWWWWWWWWWW”

示例3:

“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW”

拆分为

“WWWWWWWWWWWWWWWWWWWW”

“WWWWWWWWWWWWWWWWWWWW”

示例4:

“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW”

拆分为

“WWWWWWWWWWWWWWWWWWWW”

“WWWWWWWWWWWWWWWWWWWW”

希望这可以帮助那里的人:)


1
投票
function split_half($string){
$result= array();
$text = explode(' ', $string);
$count = count($text);
$string1 = '';
$string2 = '';
if($count > 1){
    if($count % 2 == 0){
        $start = $count/2;
        $end = $count;
        for($i=0; $i<$start;$i++){
            $string1 .= $text[$i]." ";
        }
        for($j=$start; $j<$end;$j++){
            $string2 .= $text[$j]." ";
        }           
    $result[] = $string1;
    $result[] = $string2;
    }
    else{
        $start = round($count/2)-1;
        $end = $count;
        for($i=0; $i<$start;$i++){
            $string1 .= $text[$i]." ";
        }
        for($j=$start; $j<$end;$j++){
            $string2 .= $text[$j]." ";
        }           
    $result[] = $string1;
    $result[] = $string2;

    }
}
else{
    $result[] = $string;
}
return $result;
}

使用此函数将字符串分割成半个单词..


1
投票

与往常一样,正则表达式使开发人员免于大量繁琐的字符串操作调用和不必要的脚本膨胀。我什至会大胆地说,正则表达式模式可能比字符串函数负载脚本更容易理解。

代码:(演示

$text = "The Quick : Brown Fox Jumped Over The Lazy / Dog";
$halfWay = (int)(strlen($text) / 2);
var_export(
    preg_split(
        '~.{0,' . $halfWay . '}\K\s~s',
        $text,
        2
    )
);

输出:

array (
  0 => 'The Quick : Brown Fox',
  1 => 'Jumped Over The Lazy / Dog',
)

实际上,我们通过计算字符串的字符数来计算字符串的中心,然后除以二并删除所有小数位。

然后贪婪地匹配 0 到

$halfWay
个字符,然后用
\K
忘记这些字符,然后在最新的限定空间上分割字符串。
preg_split()
的第三个参数决定了可以产生的元素的最大数量。


0
投票

我创建了一个很好的解决方案,我们不会丢失字符或单词不会被错误地剪切。

function split_title_middle ( $title ) {
    $title = strip_tags( $title );
    $middle_length = floor( strlen( $title ) / 2 );
    $new_title = explode( '<br />', wordwrap( $title, $middle_length, '<br />') );
    if (isset( $new_title[2] ) ) {
        $new_title[1] .= ' ' . $new_title[2];
        unset( $new_title[2] );
    }

    return $new_title;
 }

// how to use
$title = split_title_middle( $title );
echo $title[0] . '<strong>' . $title[1] . '</strong>';

0
投票

我尝试使用其中的几个答案,但没有得到最好的结果,所以我想我会分享解决方案。我想将标题分成两半并显示一半白色,一半绿色。

我最终将字数、文本长度、中间点和字符串的三分之一结合起来。这使我能够确保白色部分位于第三路/中路标记之间。

我希望它对某人有帮助。请注意,在我的代码中 -,& 等将被视为一个词 - 它在我的测试中没有给我带来问题,但如果我发现它没有按我希望的方式工作,我会更新它。

public function splitTitle($text){

    $words = explode(" ",$text);
    $strlen = strlen($text);
    $halfwaymark = ceil($strlen/2);
    $thirdwaymark = ceil($strlen/3);

    $wordcount = sizeof($words);
    $halfwords = ceil($wordcount/2);
    $thirdwords = ceil($wordcount/3);

    $string1 ='';
    $wordstep = 0;
    $wordlength = 0;


    while(($wordlength < $wordcount && $wordstep < $halfwords) || $wordlength < $thirdwaymark){
        $string1 .= $words[$wordstep].' ';
        $wordlength = strlen($string1);
        $wordstep++;
    }

    $string2 ='';
    $skipspace = 0;
    while(($wordstep < $wordcount)){
        if($skipspace==0) {
            $string2 .= $words[$wordstep];
        } else {
            $string2 .= ' '.$words[$wordstep];
        }
        $skipspace=1;
        $wordstep++;
    }

    echo $string1.' <span class="highlight">'.$string2.'</span>';

}

0
投票
$length = strlen($text);
$middle = intdiv($length, 2);

if ($length % 2 === 0) {
    return substr($text, $middle - 1, 2);
} else {
    return $text[$middle];
}

-1
投票

只需更改线路:

$half = (int)ceil(count($words = (count(explode(" ",$text))) / 2);

str_word_count()
可能不能算作 : 或 / 作为单词。

© www.soinside.com 2019 - 2024. All rights reserved.