PHP limits text string not including html tags?

Here is what does NOT work 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\ 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() also enables invisible text in the limit ( <a href="http://www.jackismydog.com"> and </a> ), which takes 41 out of 100 available characters.

Is there a way to limit the text so that the word "Jack" from the link is included in the limit, but not <a href="http://www.jackismydog.com"> or </a> ?

Edit: I want to keep the link in a string, just don't count it to the limit.

+7
php substr
source share
5 answers

The function of truncating 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

+4
source share

The easiest way is to actually analyze this in the DOM structure. You can use DOMDocument . Then you can simply view the elements and make any changes to the content.

Another approach would be to search and replace the regular expression with two passes - first use the regular expression to find the contents of the tags, and then use the regular expression to replace the contents with the abbreviated content. This can be achieved with the usual preg_ * functions.

+3
source share

If you want to limit the text part, you need to analyze it and check the limit yourself. The easiest way:

 if ( strlen(strip_tags($string)) > 100 ) { // the url inside $url is too big } else { // the url inside $url fits } 
+2
source share

Not easy - you could, of course, use strip_tags to delete a line, but there is no simple fix besides this.

+2
source share

You could try this, worked for me, if there are no tags in the $ differ tag, the value will be 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\ 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; ?> 
+1
source share

All Articles