root/trunk/lib/symfonians/SymfoniansToolkit.class.php

Revision 2, 1.8 kB (checked in by nperriault, 10 months ago)

First commit of the extracted code from production, I hope no passwd has been forgotten :-)

Line 
1 <?php
2 /**
3  * Symfonians toolkit
4  *
5  * @package utils
6  */
7 class SymfoniansToolkit
8 {
9  
10   /**
11    * Very dirty way to replace / starting links by domain abs ones
12    *
13    * @param  string $html
14    * @param  string $domain
15    * @return string
16    */
17   public static function absolutizeLinks($html, $domain)
18   {
19     return str_replace('href="/', 'href="http://'.$domain.'/', $html);
20   }
21  
22   /**
23    * Removes carriage returns, tabs and double spaces
24    *
25    * @param  string $string
26    * @return string
27    */
28   public static function onelinize($string)
29   {
30     $string = strip_tags(trim($string));
31     $string = str_replace(array("\n", "\r", "\t"),
32                           array(' ', ' ', ' '),
33                           $string);
34     while (preg_match('/  /', $string))
35     {
36       $string = str_replace('  ', ' ', $string);
37     }
38     return $string;
39   }
40  
41   /**
42    * Transforms url in a text into HTML links
43    *
44    * @param  string $text
45    * @return string
46    */
47   public static function transformUrls($text)
48   {
49     //Transform URL and e-mails into links
50     $text = eregi_replace(
51        "(^|[ \t\r\n\(:,]+)?". //punctuation before URL
52        //Protocole
53        "((ftp|http|https|gopher|mailto|pop|smtp|news|nntp|telnet|wais|".
54        "file|imap|prospero|peercast|ed2k|irc|aim|mime|ftam|pnm|rtsp|ldap):".
55        "[A-Za-z0-9/](([A-Za-z0-9$|.+!*()\.,;/?:@&~=_-])|%[A-Fa-f0-9]{2})+".
56        "[A-Za-z0-9/&#=_-]+)". //valid chars at the end of an URL
57        "([\.\),:;? \t\r\n]+([^A-Za-z0-9/&%#=_-]|$))?", //punctuation not followed by valid char
58      "\\1<a href='\\2'>\\2</a>\\6", $text);
59     
60     $text = eregi_replace("(^|[ \t\r\n\(,<>:]+)?".
61        "([_a-z0-9-]+[^:/=](\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+(\?[&;A-Za-z0-9=_-]+)?)".
62        "([\.\),:;<>? \t\r\n]+([^A-Za-z0-9/&%#=_-]|$))?",
63      "\\1<a href=\"mailto:\\2\">\\2</a>\\6", $text);
64     
65     return $text;
66   }
67  
68 }
Note: See TracBrowser for help on using the browser.