| 437 | | PREG_SPLIT_DELIM_CAPTURE); |
| 438 | | $wordCount = 0; |
| 439 | | # Balance tags that need balancing. We don't have strict XHTML |
| 440 | | # coming from OpenOffice (oh, if only) so we'll have to keep a |
| 441 | | # list of the tags that are containers. |
| 442 | | $open = array(); |
| 443 | | $result = ""; |
| 444 | | $count = 0; |
| 445 | | foreach ($words as $word) { |
| 446 | | if ($count >= $word_limit) { |
| 447 | | break; |
| 448 | | } elseif (preg_match("/\<.*?\/\>/", $word)) { |
| 449 | | # XHTML non-container tag, we don't have to guess |
| 450 | | $result .= $word; |
| 451 | | continue; |
| 452 | | } elseif (preg_match("/\<(\w+)/s", $word, $matches)) { |
| 453 | | $tag = $matches[1]; |
| 454 | | $result .= $word; |
| 455 | | if (isset(aHtml::$nonContainerTags[$tag])) { |
| 456 | | continue; |
| 457 | | } |
| 458 | | $open[] = $tag; |
| 459 | | } elseif (preg_match("/\<\/(\w+)/s", $word, $matches)) { |
| 460 | | $tag = $matches[1]; |
| 461 | | if (!count($open)) { |
| 462 | | # Groan, extra close tag, ignore |
| 463 | | continue; |
| 464 | | } |
| 465 | | $last = array_pop($open); |
| 466 | | if ($last !== $tag) { |
| 467 | | # They closed the wrong tag. Again, ignore for now, but |
| 468 | | # we might want to work on a better solution |
| 469 | | continue; |
| 470 | | } |
| 471 | | $result .= $word; |
| 472 | | } elseif (preg_match("/^\s+$/s", $word)) { |
| 473 | | $result .= $word; |
| 474 | | } else { |
| 475 | | if (strlen($word)) { |
| 476 | | $count++; |
| 477 | | $result .= $word; |
| 478 | | } |
| 479 | | } |
| 480 | | } |
| 481 | | for ($i = count($open) - 1; ($i >= 0); $i--) { |
| 482 | | $result .= "</" . $open[$i] . ">"; |
| 483 | | } |
| 484 | | return $result; |
| 485 | | } |
| | 437 | PREG_SPLIT_DELIM_CAPTURE); |
| | 438 | $wordCount = 0; |
| | 439 | # Balance tags that need balancing. We don't have strict XHTML |
| | 440 | # coming from OpenOffice (oh, if only) so we'll have to keep a |
| | 441 | # list of the tags that are containers. |
| | 442 | $open = array(); |
| | 443 | $result = ""; |
| | 444 | $count = 0; |
| | 445 | $num_words = count($words); |
| | 446 | foreach ($words as $word) { |
| | 447 | if ($count >= $word_limit) { |
| | 448 | break; |
| | 449 | } elseif (preg_match("/\<.*?\/\>/", $word)) { |
| | 450 | # XHTML non-container tag, we don't have to guess |
| | 451 | $result .= $word; |
| | 452 | continue; |
| | 453 | } elseif (preg_match("/\<(\w+)/s", $word, $matches)) { |
| | 454 | $tag = $matches[1]; |
| | 455 | $result .= $word; |
| | 456 | if (isset(aHtml::$nonContainerTags[$tag])) { |
| | 457 | continue; |
| | 458 | } |
| | 459 | $open[] = $tag; |
| | 460 | } elseif (preg_match("/\<\/(\w+)/s", $word, $matches)) { |
| | 461 | $tag = $matches[1]; |
| | 462 | if (!count($open)) { |
| | 463 | # Groan, extra close tag, ignore |
| | 464 | continue; |
| | 465 | } |
| | 466 | $last = array_pop($open); |
| | 467 | if ($last !== $tag) { |
| | 468 | # They closed the wrong tag. Again, ignore for now, but |
| | 469 | # we might want to work on a better solution |
| | 470 | continue; |
| | 471 | } |
| | 472 | $result .= $word; |
| | 473 | } elseif (preg_match("/^\s+$/s", $word)) { |
| | 474 | $result .= $word; |
| | 475 | } else { |
| | 476 | if (strlen($word)) { |
| | 477 | $count++; |
| | 478 | $result .= $word; |
| | 479 | } |
| | 480 | } |
| | 481 | } |
| | 482 | |
| | 483 | $append_ellipsis = false; |
| | 484 | if (isset($options['append_ellipsis'])) |
| | 485 | { |
| | 486 | $append_ellipsis = $options['append_ellipsis']; |
| | 487 | } |
| | 488 | if ($append_ellipsis == true && $num_words > $word_limit) |
| | 489 | { |
| | 490 | $result .= '…'; |
| | 491 | } |
| | 492 | |
| | 493 | for ($i = count($open) - 1; ($i >= 0); $i--) { |
| | 494 | $result .= "</" . $open[$i] . ">"; |
| | 495 | } |
| | 496 | return $result; |
| | 497 | } |