[英]PHP limit text string NOT including html tags?
Here's what's NOT working for me: 这是什么不适合我:
<?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;
?>
I want to limit the VISIBLE text to 100 characters, but using substr()
is also including the non-visible text in the limit ( <a href="http://www.jackismydog.com">
and </a>
) which takes up 41 of those available 100 characters. 我想将VISIBLE文本限制为100个字符,但使用
substr()
还包括限制中的不可见文本( <a href="http://www.jackismydog.com">
和</a>
)它占用了100个可用字符中的41个。
Is there a way to limit the text so that the word "Jack" from the link would be included in the limit, but not <a href="http://www.jackismydog.com">
or </a>
? 有没有办法限制文本,以便链接中的单词“杰克”将包含在限制中,但不是
<a href="http://www.jackismydog.com">
或</a>
?
Edit: I want to keep the link in the string, just not count it's length towards the limit.. 编辑:我想保留字符串中的链接,只是不计算它的限制长度..
A function to truncate words in HTML code: 截断HTML代码中单词的函数:
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com
function truncate($text, $length, $suffix = '…', $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 资料来源: http : //snippets.dzone.com/posts/show/7125
不容易 - 你当然可以使用strip_tags去除字符串,但除此之外没有简单的解决方法。
The easiest way would be to actually parse this into a DOM structure. 最简单的方法是将其解析为DOM结构。 You could use DOMDocument for that.
您可以使用DOMDocument 。 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.
这可以通过常用的preg_ *函数实现。
If you want to limit the text part, you need to parse it and check the limit yourself. 如果要限制文本部分,则需要解析它并自行检查限制。 The easiest way is to:
最简单的方法是:
if ( strlen(strip_tags($string)) > 100 )
{
// the url inside $url is too big
}
else
{
// the url inside $url fits
}
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 您可以试试这个,如果没有标签在字符串中,我可以使用$不同的值将为0,给$ stringsize你的原始值为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;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.