Salve a tutti ....stavo cercando di dividere una stringa tramite php e mi sono imbattuto in questo script che potete trovare pure alla pagina http://dodona.wordpress.com/2009/04/...the-html-code/
il problema è che non riesco ad usarlo , inoltre è tutto in inglese...insomma proteste darmi una mano ???
Codice PHP:
<?php
//@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 truncate($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]); }
e continua e continua ... potete trovarlo tutto sul link postato