使用PHP将字符串一分为二(支持Word) 我试图把字符串分成两半,它不应该在一个词的中间分裂。

使用PHP将字符串一分为二(支持Word) 我试图把字符串分成两半,它不应该在一个词的中间分裂。,php,string,split,Php,String,Split,到目前为止,我提出了以下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, $h

到目前为止,我提出了以下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
拆分后,我需要在字符串中保留所有符号,如:和/等。我不明白为什么当前代码正在删除符号。。。如果您可以提供替代方法或修复此方法以不删除符号,我们将不胜感激:)

演示:

只需更改行:

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

str\u word\u count()
可能不计算:或/as word。

在查看示例输出时,我注意到我们所有的示例都是关闭的,如果字符串的中间在一个单词内,我们会给string1更少的值,而不是给更多的值

例如:<代码>中:Brown Fox:跳过懒惰/狗< /代码>是代码>快:Brown Fox Ju < /代码>,在一个词的中间,这个第一个例子给出Srang2分裂的词;下面的示例为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"
$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"
在拆分单词时为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"
$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"
函数拆分(字符串){
$result=array();
$text=分解(“”,$string);
$count=计数($text);
$string1='';
$string2='';
如果($count>1){
如果($count%2==0){
$start=$count/2;
$end=$count;

对于($i=0;$i我创建了一个很好的解决方案,在这个解决方案中,我们不会丢失字符,也不会错误地剪切单词

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>';
函数拆分\标题\中间($title){
$title=带标签($title);
$middle_length=楼层(strlen($title)/2);
$new_title=explode('
',wordwrap($title,$medium_length,'
'); if(isset($new_title[2])){ $new_title[1]。=''.$new_title[2]; 未设置($new_title[2]); } 返回$new_title; } //如何使用 $title=拆分\标题\中间($title); 回显$title[0]。。$title[1]。
我知道这是一个老问题,但我有下面的代码可以做需要做的事情

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

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:

“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW”

被分割为

“WWWWWWWWWWWWWWWW”

“WWWWWWWWWWWWWWWW”

示例2:

“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW”

被分割为

“WWWWWWWWWWWWWWWW”

“WWWWWWWWWWWWWWWW”

示例3:

“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW”

被分割为

“WWWWWWWWWWWWWWWW”

“WWWWWWWWWWWWWWWW”

示例4:

“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

被分割为

“WWWWWWWWWWWWWWWW”

“WWWWWWWWWWWWWWWW”


希望这能对其他人有所帮助:)

我尝试使用了一些答案,但没有得到最好的结果,所以我想与大家分享解决方案。我想将我的标题一分为二,一半显示白色,一半显示绿色

最后,我结合了字数、文本长度、中间点和字符串的三分之一。这使我能够确保白色部分位于第三个中间点/中间点之间

我希望它能帮助一些人。请注意,在我的代码中-,等等将被视为一个词-它不会在我的测试中给我带来问题,但如果我发现它没有像我希望的那样工作,我会更新它

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

}
公共函数拆分标题($text){
$words=分解(“,$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;
而($wordstep<$wordcount)){
如果($skipspace==0){
$string2.=$words[$wordstep];
}否则{
$string2.=''.$words[$wordstep];
}
$skipspace=1;
$wordstep++;
}
回显$string1.'.$string2';
}

与往常一样,正则表达式为开发人员节省了大量繁琐的字符串操作调用和不必要的脚本膨胀。我甚至会冒险说,正则表达式模式可能更容易理解,因为字符串函数加载了脚本

代码:()

输出:

array (
  0 => 'The Quick : Brown Fox',
  1 => 'Jumped Over The Lazy / Dog',
)
实际上,我们通过计算字符串的字符数来计算字符串的中心,然后除以2并删除任何小数点


然后贪婪地将零与
$Middle
字符数匹配,然后用
\K
忘记这些字符,然后在最新的限定空格上拆分字符串。
preg_split()的第三个参数
确定可能产生的元素的最大数量。

不幸的是,这不起作用,它只创建了空字符串。而且它在结尾缺少一个结束括号。但是感谢您的尝试:)感谢您提供了一个带有演示的函数,它看起来确实按预期工作。除了这个,其他函数都没有为我工作。我制作了一个slight更改修复了只有两个单词的问题,第二个单词更长,在
爆炸后添加
if(count($tmp)<3){return$tmp;}
此答案缺少教育性解释。谢谢,它可以
array (
  0 => 'The Quick : Brown Fox',
  1 => 'Jumped Over The Lazy / Dog',
)