File indexing completed on 2024-11-17 05:19:46

0001 <?php
0002 
0003 /**
0004  * Flooer Framework
0005  *
0006  * LICENSE: BSD License (2 Clause)
0007  *
0008  * @category    Flooer
0009  * @package     Flooer_Utility
0010  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0011  * @copyright   Akira Ohgaki
0012  * @license     https://opensource.org/licenses/BSD-2-Clause  BSD License (2 Clause)
0013  * @link        https://github.com/akiraohgaki/flooer
0014  */
0015 
0016 /**
0017  * Usage
0018  *
0019  * $convertedText = Flooer_Utility_Text::convert($text);
0020  */
0021 
0022 /**
0023  * Text converter class
0024  *
0025  * @category    Flooer
0026  * @package     Flooer_Utility
0027  * @author      Akira Ohgaki <akiraohgaki@gmail.com>
0028  */
0029 class Flooer_Utility_Text
0030 {
0031 
0032     /**
0033      * Batch converting
0034      *
0035      * @param   string $string
0036      * @param   string $newline
0037      * @return  string
0038      */
0039     public static function convert($string, $newline = "\n")
0040     {
0041         $string = self::convertNewline($string, $newline);
0042         $string = self::convertUri($string);
0043         $string = self::convertEmail($string);
0044         return $string;
0045     }
0046 
0047     /**
0048      * Convert a newline
0049      *
0050      * @param   string $string
0051      * @param   string $newline
0052      * @return  string
0053      */
0054     public static function convertNewline($string, $newline = "\n")
0055     {
0056         $string = str_replace(array("\r\n", "\r"), "\n", $string);
0057         if ($newline != "\n") {
0058             $string = str_replace("\n", $newline, $string);
0059         }
0060         return $string;
0061     }
0062 
0063     /**
0064      * Convert a URI address
0065      *
0066      * @param   string $string
0067      * @return  string
0068      */
0069     public static function convertUri($string)
0070     {
0071         return preg_replace(
0072             "/(https?|ftps?|davs?|file)(:\/\/[\w\.\~\-\/\?\&\+\=\:\;\@\%\,]+)/",
0073             "<a href=\"$0\">$0</a>",
0074             $string
0075         );
0076     }
0077 
0078     /**
0079      * Convert an email address
0080      *
0081      * @param   string $string
0082      * @return  string
0083      */
0084     public static function convertEmail($string)
0085     {
0086         return preg_replace(
0087             "/([\.\+\w\_\-]+@[\.\w\-]+\.[a-zA-Z]+)/",
0088             "<a href=\"mailto:$0\">$0</a>",
0089             $string
0090         );
0091     }
0092 
0093 }