9

これが私のために働いていないものです:

<?php
 $string = 'I have a dog and his name is <a href="http://www.jackismydog.com">Jack</a> and I love him very much because he\'s my favorite dog in the whole wide world and nothing could make me not love him, I think.';
 $limited = substr($string, 0, 100).'...';
 echo $string;
?>

VISIBLEテキストを100文字に制限したいのですが、使用可能な100文字のうち41文字を占める制限(および)substr()に非表示テキストも含まれています。<a href="http://www.jackismydog.com"></a>

リンクからの「ジャック」という単語が制限に含まれるが、含まれないようにテキストを制限する方法はあります<a href="http://www.jackismydog.com"></a>

編集:リンクの長さを制限まで数えないで、リンクを文字列に保持したい。

4

5 に答える 5

5

A function to truncate words in HTML code:

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com
function truncate($text, $length, $suffix = '&hellip;', $isHTML = true) {
    $i = 0;
    $simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);
    $tags = array();
    if($isHTML){
        preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
        foreach($m as $o){
            if($o[0][1] - $i >= $length)
                break;
            $t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
            // test if the tag is unpaired, then we mustn't save them
            if($t[0] != '/' && (!isset($simpleTags[$t])))
                $tags[] = $t;
            elseif(end($tags) == substr($t, 1))
                array_pop($tags);
            $i += $o[1][1] - $o[0][1];
        }
    }

    // output without closing tags
    $output = substr($text, 0, $length = min(strlen($text),  $length + $i));
    // closing tags
    $output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '');

    // Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">)
    $pos = (int)end(end(preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));
    // Append closing tags to output
    $output.=$output2;

    // Get everything until last space
    $one = substr($output, 0, $pos);
    // Get the rest
    $two = substr($output, $pos, (strlen($output) - $pos));
    // Extract all tags from the last bit
    preg_match_all('/<(.*?)>/s', $two, $tags);
    // Add suffix if needed
    if (strlen($text) > $length) { $one .= $suffix; }
    // Re-attach tags
    $output = $one . implode($tags[0]);

    //added to remove  unnecessary closure
    $output = str_replace('</!-->','',$output); 

    return $output;
}

Source: http://snippets.dzone.com/posts/show/7125

于 2011-12-13T08:03:02.597 に答える
3

The easiest way would be to actually parse this into a DOM structure. You could use DOMDocument for that. Then you could simply go through the elements and make any changes to content.

Another approach would be to do a two-pass regex search and replace - first use the regex to find contents of tags, then use the regex to replace the contents with shortened contents. This can be achieved with your usual preg_* functions.

于 2010-07-01T21:05:12.030 に答える
2

テキスト部分を制限したい場合は、それを解析して自分で制限を確認する必要があります。最も簡単な方法は次のとおりです。

if ( strlen(strip_tags($string)) > 100 )
{
    // the url inside $url is too big
}
else
{
    // the url inside $url fits
}
于 2010-07-01T21:03:02.920 に答える
2

簡単ではありません-もちろん、strip_tagsを使用して文字列を非HTML化することはできますが、それ以外に簡単な修正はありません。

于 2010-07-01T21:04:10.953 に答える
1

You could try this, worked for me if no tags are in string $differ will have a value of 0 giving $stringsize your original value of 100

  <?php
$string = 'I have a dog and his name is <a href="http://www.jackismydog.com">Jack</a> and I love him very much because he\'s my favorite dog in the whole wide world and nothing could make me not love him, I think.';

$stringall=strlen($string);
$striphtml = strip_tags($string);
$stringnohtml=strlen(striphtml);
$differ=($stringall-$stringnohtml);
$stringsize=($differ + 100);
$limited = substr($string, 0, $stringsize).'...';
echo $limited;
?>
于 2014-02-24T02:18:01.080 に答える