Rumah  >  Artikel  >  pembangunan bahagian belakang  >  一道php字符串截取的面试题

一道php字符串截取的面试题

WBOY
WBOYasal
2016-09-21 14:13:10891semak imbas

<code>$str = '这是<div>一道<a href="http://www.baidu.com">php字符串</a>截取题</div>。';
</code>

将以上字符串截取前7个字符显示出来,最终应该要这个结果:

<code>'这是<div>一道<a href="http://www.baidu.com">php</a>
</div>'
</code>

要求:

  1. 如果字符串中有HTML标签就略过不记数

  2. 如果截取完之后有HTML标签被截断了,那么要在最后把截断的标签再补上结束标签

回复内容:

<code>$str = '这是<div>一道<a href="http://www.baidu.com">php字符串</a>截取题</div>。';
</code>

将以上字符串截取前7个字符显示出来,最终应该要这个结果:

<code>'这是<div>一道<a href="http://www.baidu.com">php</a>
</div>'
</code>

要求:

  1. 如果字符串中有HTML标签就略过不记数

  2. 如果截取完之后有HTML标签被截断了,那么要在最后把截断的标签再补上结束标签

没有推测题目的用途,单纯按要求一写了个正则替换

<code class="php">function pure_cut($str, $len) {
    $reg = '/' . str_repeat('[^]((?:]+>)+)?', $len) . '$/u';
    $str = preg_replace_callback($reg, function($matches) {
        array_shift($matches);
        $replace = join('', $matches);
        return $replace;
    }, $str, 7);
    return $str;
}

echo pure_cut($str, 7);</code>

不过要求2 没太明白。在要求1满足的情况下,html 标签是不会被破坏的,不需要专门去修复啊。

应该是截取富文本编辑框的内容吧.

<code class="php"><?php $str = '这是<div>一道<a href="http://www.baidu.com">php字符串</a>截取题。';

function truncate($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) {
    if ($considerHtml) {
            if (mb_strlen(strip_tags($text)) )?([^]*)/s', $text, $lines, PREG_SET_ORDER);
            $total_length = mb_strlen($ending);
            $open_tags = array();
            $truncate = '';

            foreach ($lines as $line_matchings) {
                    if (!empty($line_matchings[1])) {
                            if (preg_match('/^$/is', $line_matchings[1])) {
                            } else if (preg_match('/^$/s', $line_matchings[1], $tag_matchings)) {
                                    $pos = array_search($tag_matchings[1], $open_tags);
                                    if ($pos !== false) {
                                    unset($open_tags[$pos]);
                                    }
                            } else if (preg_match('/^!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
                                    array_unshift($open_tags, strtolower($tag_matchings[1]));
                            }
                            $truncate .= $line_matchings[1];
                    }
                    $content_length = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
                    if ($total_length+$content_length > $length) {
                            $left = $length - $total_length;
                            $entities_length = 0;
                            if (preg_match_all('/&[0-9a-z]{2,8};|[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
                                    foreach ($entities[0] as $entity) {
                                            if ($entity[1]+1-$entities_length = $length) {
                            break;
                    }
            }
    } else {
            if (mb_strlen($text) ';
            }
    }
    return $truncate;
}

echo truncate($str, 7, '', true, true);</code>
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn