|
Follow @phpsnips |
<?php
/*
* Date: 01/11/12
* Shorten text & close unclosed tags
* Snippet: http://adamlacombe.com/shorten-text-close-unclosed-tags/4
* Demo: http://adamlacombe.com/
*
* Adam LaCombe
* CC BY-NC-SA 3.0
* http://creativecommons.org/licenses/by-nc-sa/3.0/
*/
function shorten_text($str, $limit='100')
{
$array_words = explode(' ', $str);
if(count($array_words) > $limit)
{
$i = 0;
$final = '';
foreach($array_words as $word){
if($limit > $i)
{
$final .= "$word ";
}
$i++;
}
$short = 1;
}
else
{
$final = $str;
}
$tags = array('center' => 'center',
'img=[^\]]*' => 'img',
'url=[^\]]*' => 'url',
'img' => 'img',
'url' => 'url',
'u' => 'u',
'i' => 'i',
'b' => 'b',
'align=[^\]]*' => 'align',
'mail=[^\]]*' => 'mail',
'font=[^\]]*' => 'font',
'size=[^\]]*' => 'size',
'color=[^\]]*' => 'color');
$matches = array();
foreach ($tags as $opentag => $closetag)
{
$closed = preg_match_all("~\[/".$closetag."\]~i", $final, $matches);
$open = preg_match_all("~\[".$opentag."\]~i", $final, $matches);
if ($open > $closed)
{
$final .= "[/".$closetag."]";
}
}
if(isset($short))
{
return "".$final."[....]";
}
else
{
return $final;
}
}
?>
|
Rate this Snippet: |
Suggested Difficulty Level: No votes yet.
Current Score: 3.67
Total Views: 631
Other top snippets by adam:
1. Automatic Color Correction (4.50 of 2)
2. Image to ASCII (4.33 of 3)
3. String Output Formatter (1.00 of 1)
4. IP Blacklist Check (5.00 of 11)
5. Send SMS with (3.60 of 5)
6. Cache PHP Generated (0.00 of 0)
7. Database Connection (4.00 of 2)
8. Email Validator (3.25 of 4)
9. Shorten text and (3.67 of 3)
10. file-based simple guest (2.00 of 1)
11. adding an entry (5.00 of 1)
12. integer to currency (1.33 of 3)
13. Aggregate IP addresses (0.00 of 0)
14. file handling (0.00 of 0)
15. testing system with (0.00 of 0)
16. Make an php (1.00 of 1)
17. allow download in (5.00 of 1)
18. redirect in 3 (3.00 of 2)
19. live clock with (1.00 of 1)
20. visit counter (4.00 of 3)
21. contact form with (3.33 of 3)
22. show a specific (1.00 of 1)
23. The & sign (5.00 of 1)
24. static variable in (1.00 of 1)
25. multiplication table with (1.00 of 1)