Файл: application/text_helper.php
Строк: 146
<?php
/**
*a text helper class
*
*@author                  The-Di-Lab
*@email                   thedilab@gmail.com
*@website                 www.the-di-lab.com
*@version               1.0
**/
class TextHelper 
{     
   /*
   * this function adds links to email address in the string
   * @param string $text text string
   * @return string with email being added with links
   */
   public function autoEmail($text) 
   {
        return preg_replace("/([a-z0-9_.-])+@(([a-z0-9-])+.)+([a-z0-9]{2,4})+/i", 
                                '<a href="mailto:$0">$0</a>', $text);
   }
   
   /*
   * this function transforms http links to hyperlinks
   * @param string $text
   * @return string $text
   */
   public function autoUrl($text) 
   {
           return preg_replace("#http://([A-z0-9./-]+)#", '<a href="$0">$0</a>', $text);
   }
   
   /*
   * this function will shorten the string by $length, and append $end after that.
   * @param string $text
   * @param int $length, restricted length
   * @param int $end, append to the end of the string after it is shortten
   * @static
   */
   public function truncate($text,  $length=100, $end='...')
   {
        if(strlen($text)>$length) {
            $text = substr($text,0,$length).$end;
        }
           return $text;
   }
      
   /*
   * this function extracts string between $start and $end in $text
   * @param string $text 
   * @param string $start 
   * @param string $end 
   * @return string between $start and $end, '' if not found
   */
   public function extractBetween($text,$start,$end) 
   {   
        if(preg_match('/'.$start.'(.*)'.$end.'/',$text,$matches)){
            return $matches[1];
        }        
        return '';           
   }
   
   /*
   * this function transforms $text to a SEO friendly urlencode string
   * @param string $text
   * @return SEO friendly string
   */
   public function toSlug($text) 
   {
           $text = strtolower($text);
           $text = trim($text);
           $text = str_replace(array('-', ' ', '&'), array('_', '-', 'and'), $text);
           return urlencode($text);
   }
   
   /*
   * strips specified tags from the string
   * @param string $text
   * @param strig  $tags  array of tags   *
   * @return string
   * @access public
   */
   public function stripTags($text,$tags) 
   {
           if(!is_array($tags)){
               $tags = array($tags);
           }
                      
           foreach($tags as $tag) {
            $text = preg_replace('/<' . $tag. 'b[^>]*>/i', '', $text);
            $text = preg_replace('/</' . $tag . '[^>]*>/i', '', $text);
        }
        
           return $text;
   }
   
   /*
   * remove img from string 
   * @param string $text
   * @return string
   * @access public
   */
   public function stripImages($text) 
   {
           $text = preg_replace('/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(</a>)/i', '$1$3$5<br />', $text);
        $text = preg_replace('/(<img[^>]+alt=")([^"]*)("[^>]*>)/i', '$2<br />', $text);
        $text = preg_replace('/<img[^>]*>/i', '', $text);       
        return $text;
   }
   
   /*
   * strip scripts and stylesheets from string
   * string  $text   
   * @return string
   * @access public
   */
   public function stripScripts($text) 
   {
           return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?</script>|<style[^>]*>.*?</style>|<!--.*?-->/i', '', $text);
   }
   
   /*
   * strip whitespace from the text
   * @param string $text
   * @return string
   * @access public
   */
   public function stripWhitespace($text) 
   {
           $r = preg_replace('/[nrt]+/', '', $text);           
        return preg_replace('/s{2,}/', ' ', $r);
   }
   
   /*
   * remove all the html open and close tags
   * @param string $text
   * @return string
   * @access public
   */
   public function noHtml($text) 
   {
           return strip_tags($text);
   }
   
   /*
   * make string safe for display as HTML
   * @param $text
   * @return string
   * @access public
   */
   public function html($text) 
   {
           $patterns = array("/&/", "/%/", "/</", "/>/", '/"/', "/'/", "/(/", "/)/", "/+/", "/-/");
        $replacements = array("&", "%", "<", ">", """, "'", "(", ")", "+", "-");
        $string = preg_replace($patterns, $replacements, $text);
           return $string;
   }
   
}