如何将字符串分成两个div?

时间:2014-02-21 12:53:54

标签: php string

我对php有一个疑问, 假设我有一个150字的字符串,我想在一个div中显示前80个字,在另一个div中显示剩余的70个字。
任何人都可以帮我解决这个问题。

提前致谢。

4 个答案:

答案 0 :(得分:1)

您可以尝试

<div ><?php echo implode(' ', array_slice(explode(' ', $str), 0, 80));?></div>
<div ><?php echo implode(' ', array_slice(explode(' ', $str), 81, 150));?></div>

答案 1 :(得分:0)

再次阅读问题之后,我就是这样做的:

    function splitByWords($string, $start = null, $end = null) {
        $words = explode(' ', $string);

        $newString = '';

        $end = $end === null ? count($words) : $end + 1;
        $start = $start === null ? 0 : $start;

        if(count($words) == 0 || count($words) < $start)
            return $string;

        for($i = $start; $i < $end; $i++)
            if(array_key_exists($i, $words))
                $newString .=  strlen($newString) !== 0 ? ' ' . $words[$i] : $words[$i];

        return $newString;
    }

    $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur quis rhoncus neque. Curabitur ultrices sem luctus interdum feugiat. Sed purus nulla, rhoncus nec diam sed, sodales semper felis. Aliquam ultrices feugiat venenatis. Maecenas eu rutrum nibh, sit amet ultrices quam. Duis justo arcu, bibendum ac velit vel, cursus accumsan erat. Duis scelerisque eget augue id consectetur. Aenean bibendum sapien vel nisl imperdiet convallis. Nam a nunc justo. Etiam porttitor euismod erat, eu volutpat lacus elementum ac. Mauris scelerisque blandit eros lacinia congue. Etiam fermentum risus quis turpis semper ultricies. Phasellus lacinia, lorem eget interdum condimentum, dolor nibh euismod augue, ut euismod diam orci non nulla.";

    $div1 = splitByWords($string, 9, 20);
    $div2 = splitByWords($string, 80);

    echo $div1;

Try it here

First method tried

答案 2 :(得分:0)

您可以使用explode()array_slice()implode()轻松完成此操作:

<?php
    function splitWords($string, $wordsPerEntry) {
        $words = explode(' ', $string);

        $return = array();

        for ($i = 0; $i < count($words); $i += $wordsPerEntry) {
            $return[] = implode(' ', array_slice($words, $i, $wordsPerEntry));
        }

        return $return;
    }
?>

<强>用法

<?php
    $loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ipsum sapien, eget consectetur felis placerat id. Suspendisse cursus eget nisi vitae aliquet. Mauris varius, metus eu feugiat mattis, magna arcu iaculis lorem, porttitor mollis ipsum justo at magna. Morbi at ligula iaculis, mollis lorem ac, bibendum purus. Mauris commodo porttitor magna. Morbi consequat ac ligula eu gravida. Cras tempus consectetur ipsum, nec convallis eros posuere ac. Aliquam luctus mi ac sapien scelerisque, et volutpat magna iaculis. Ut non risus tempor, aliquam felis et, placerat mi. Curabitur lacinia, nibh sed fermentum congue, velit velit tincidunt diam, sit amet scelerisque nisi leo at massa. Pellentesque porta mauris in turpis congue, convallis ornare mi porta. Vivamus semper iaculis sagittis. Etiam id nisi at sem rutrum aliquam. Duis tellus tellus, accumsan quis congue facilisis, ullamcorper quis leo. Donec commodo consectetur odio at porttitor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames.';

    $split = splitWords($loremIpsum, 80);

    print_r($split);

    foreach ($split as $line) {
        var_dump(count(explode(' ', $line)));
    }
?>

<强>输出

Array (
    [0] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ipsum sapien, eget consectetur felis placerat id. Suspendisse cursus eget nisi vitae aliquet. Mauris varius, metus eu feugiat mattis, magna arcu iaculis lorem, porttitor mollis ipsum justo at magna. Morbi at ligula iaculis, mollis lorem ac, bibendum purus. Mauris commodo porttitor magna. Morbi consequat ac ligula eu gravida. Cras tempus consectetur ipsum, nec convallis eros posuere ac. Aliquam luctus mi ac sapien scelerisque, et volutpat magna iaculis. Ut non risus
    [1] => tempor, aliquam felis et, placerat mi. Curabitur lacinia, nibh sed fermentum congue, velit velit tincidunt diam, sit amet scelerisque nisi leo at massa. Pellentesque porta mauris in turpis congue, convallis ornare mi porta. Vivamus semper iaculis sagittis. Etiam id nisi at sem rutrum aliquam. Duis tellus tellus, accumsan quis congue facilisis, ullamcorper quis leo. Donec commodo consectetur odio at porttitor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames. 
)    

int(80) //words in $split[0]
int(70) //words in $split[1]

DEMO

答案 3 :(得分:0)

你可以使用一个{x,x}重复的RegExp指定恰好80个字的匹配,然后恰好70个字的匹配。

//string
$sStringOf150Words = " /* your string here */ ";

//pattern match
$reMatch = '/^((?:[^\s]+\s?){80})\s((?:[^\s]+\s?){70})$/';

//matches
$sFirst80Words = preg_replace($reMatch, "$1", $sStringOf150Words);
$sNext70Words = preg_replace($reMatch, "$2", $sStringOf150Words);

//output
echo "<div>{$sFirst80Words}</div>"
   . "<div>{$sNext70Words}</div>";