Hi, I have this nice function to truncate a string with HTML.
It's very good only now I ran into a problem.
There are cases where for example The input will be this:
<a href="some url">this is a link</a>
and what I want to output is exactly the same, even if it it's bigger than the maximum length specified.
In other words, inside html tags it can't cut.
Can someone help me, please?
I've searched for a solution and thought about it for quite some time.. - <?php
-
/**
-
* truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags
-
*
-
* @param string $text String to truncate.
-
* @param integer $length Length of returned string, including ellipsis.
-
* @param string $ending Ending to be appended to the trimmed string.
-
* @param boolean $exact If false, $text will not be cut mid-word
-
* @param boolean $considerHtml If true, HTML tags would be handled correctly
-
*
-
* @return string Trimmed string.
-
*/
-
function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) {
-
if ($considerHtml) {
-
// if the plain text is shorter than the maximum length, return the whole text
-
if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
-
return $text;
-
}
-
// splits all html-tags to scanable lines
-
preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
-
$total_length = strlen($ending);
-
$open_tags = array();
-
$truncate = '';
-
foreach ($lines as $line_matchings) {
-
// if there is any html-tag in this line, handle it and add it (uncounted) to the output
-
if (!empty($line_matchings[1])) {
-
// if it's an "empty element" with or without xhtml-conform closing slash
-
if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
-
// do nothing
-
// if tag is a closing tag
-
} else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
-
// delete tag from $open_tags list
-
$pos = array_search($tag_matchings[1], $open_tags);
-
if ($pos !== false) {
-
unset($open_tags[$pos]);
-
}
-
// if tag is an opening tag
-
} else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
-
// add tag to the beginning of $open_tags list
-
array_unshift($open_tags, strtolower($tag_matchings[1]));
-
}
-
// add html-tag to $truncate'd text
-
$truncate .= $line_matchings[1];
-
}
-
// calculate the length of the plain text part of the line; handle entities as one character
-
$content_length = 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) {
-
// the number of characters which are left
-
$left = $length - $total_length;
-
$entities_length = 0;
-
// search for html entities
-
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)) {
-
// calculate the real length of all entities in the legal range
-
foreach ($entities[0] as $entity) {
-
if ($entity[1]+1-$entities_length <= $left) {
-
$left--;
-
$entities_length += strlen($entity[0]);
-
} else {
-
// no more characters left
-
break;
-
}
-
}
-
}
-
$truncate .= substr($line_matchings[2], 0, $left+$entities_length);
-
// maximum lenght is reached, so get off the loop
-
break;
-
} else {
-
$truncate .= $line_matchings[2];
-
$total_length += $content_length;
-
}
-
// if the maximum length is reached, get off the loop
-
if($total_length>= $length) {
-
break;
-
}
-
}
-
} else {
-
if (strlen($text) <= $length) {
-
return $text;
-
} else {
-
$truncate = substr($text, 0, $length - strlen($ending));
-
}
-
}
-
// if the words shouldn't be cut in the middle...
-
if (!$exact) {
-
// ...search the last occurance of a space...
-
$spacepos = strrpos($truncate, ' ');
-
if (isset($spacepos)) {
-
// ...and cut the text in this position
-
$truncate = substr($truncate, 0, $spacepos);
-
}
-
}
-
// add the defined ending to the text
-
$truncate .= $ending;
-
if($considerHtml) {
-
// close all unclosed html-tags
-
foreach ($open_tags as $tag) {
-
$truncate .= '</' . $tag . '>';
-
}
-
}
-
return $truncate;
-
}
-
-
?>
1 3434 Luuk 1,047
Expert 1GB
This cannnot be done, because everything is inside the HTML-tag 'html'.....
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sheela |
last post by:
Hi all gurus in tha club,
I scripted a prog that extract a string from an html page excluding
all the tags.
The problem is that it works quite slowly and I wanted to know if
somebody of us as an...
|
by: Jason Kester |
last post by:
I have a UserControl living on MyPage.aspx. How would MyPage.aspx go
about retrieving the HTML content generated by that UserControl as a
string? I've looked into two options, neither of which...
|
by: John Dalberg |
last post by:
Why does IE not show the full html source when I try to view the source?? I
mean why does it hide a few features.
Previously I had an issue where the data grid html representation was
hidden in...
|
by: Alex |
last post by:
Hello.
First, with AJAX I will get a remote web page into a string. Thus, a
string will contain HTML tags and such. I will need to extract text
from one <span> for which I know the ID the inner...
|
by: Sam |
last post by:
Hi All,
Is it possible to get a web page content of one of my webpages using
relative path?
for example:
If I have a website which consists of 2 pages: testpage1.aspx and
testpage2.aspx...
|
by: =?Utf-8?B?TW9uaWNh?= |
last post by:
Dear reader,
how can I keep my password content when a postback occur?
I define an edit box in password textmode. but when a postback occur and my
page became refresh, its content would be...
|
by: gagandeepgupta16 |
last post by:
I am having problem in parsing a string containing HTML Tags.
The situation is somewhat similar to the following as quoted in some other forum :
if (typeof DOMParser == "undefined") {
...
|
by: cardei |
last post by:
Hi, I try to retrieve html content from a web page with VB .net
I used this code (console app) that I find on internet :
Imports System
Imports System.Net
Imports System.IO
Module...
|
by: arindams |
last post by:
I've a form where an webrowser contains a html file. I want to copy a selective portion of the html content and paste that content in an excel 2007 file where it will be pasted as a normal text (not...
|
by: Andy B |
last post by:
How do you add html content to a string and make it renter in the browser as
html? Do you just add the html markup to it?
Example would be something like:
<asp:CustomControl ID="Control"...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |