[pLog-svn] r5285 - in plog/branches/lifetype-1.2/class: data test/tests/data

jondaley at devel.lifetype.net jondaley at devel.lifetype.net
Sat Apr 7 16:15:17 EDT 2007


Author: jondaley
Date: 2007-04-07 16:15:17 -0400 (Sat, 07 Apr 2007)
New Revision: 5285

Modified:
   plog/branches/lifetype-1.2/class/data/textfilter.class.php
   plog/branches/lifetype-1.2/class/test/tests/data/textfilter_test.class.php
Log:
combine urlize and domainize so when we make changes, we can more easily make changes to both, since the functions are so similar.  Added removing separator duplicates to domainize.  Instead of removing bad characters in strings, replace them with a separator - so if input is 'Pittsburgh,PA', the output is 'pittsburgh-pa' instead of 'pittsburghpa'.  Updated tests to reflect new changes

Modified: plog/branches/lifetype-1.2/class/data/textfilter.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/data/textfilter.class.php	2007-04-07 19:13:31 UTC (rev 5284)
+++ plog/branches/lifetype-1.2/class/data/textfilter.class.php	2007-04-07 20:15:17 UTC (rev 5285)
@@ -420,33 +420,55 @@
          *
          * @param string The string that we wish to convert into something that can be used as a URL
          */
-        function urlize( $string )
+        function urlize( $string, $domainize = false )
         {
-		    // remove unnecessary spaces and make everything lower case
+            lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
+			$config =& Config::getConfig();
+            $separator = $config->getValue( "urlize_word_separator", URLIZE_WORD_SEPARATOR_DEFAULT );
+
+            // remove unnecessary spaces and make everything lower case
 		    $string = preg_replace( "/ +/", " ", strtolower($string) );
 
             // removing a set of reserved characters (rfc2396: ; / ? : @ & = + $ ,)
-            $string = str_replace(array(';','/','?',':','@','&','=','+','$',','), '', $string);
+            $string = str_replace(array(';','/','?',':','@','&','=','+','$',','),
+                                  $separator, $string);
 
             // replace some characters to similar ones
             $search  = array(' ', 'ä', 'ö', 'ü','é','è','à','ç', 'à', 'è', 'ì',
                              'ò', 'ù', 'á', 'é', 'í', 'ó', 'ú', 'ë', 'ï' );
-            lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
-			$config =& Config::getConfig();
-            $separator = $config->getValue( "urlize_word_separator", URLIZE_WORD_SEPARATOR_DEFAULT );
-            
-            $replace = array( $separator , 'a','o','u','e','e','a','c', 'a', 'e', 'i',
+            $replace = array( $separator, 'a','o','u','e','e','a','c', 'a', 'e', 'i',
                               'o', 'u', 'a', 'e', 'i', 'o', 'u', 'e', 'i' );
+            if($domainize){
+                    // domains shouldn't have underscores, and we'll convert
+                    // hyphens to the user-customizable $separator to be
+                    // consistent
+                $search[] = '-';
+                $search[] = '_';
+                $replace[] = $separator;
+                $replace[] = $separator;
+            }
             $string = str_replace($search, $replace, $string);
             
-            // and everything that is still left that hasn't been replaced/encoded, throw it away
-            $string = preg_replace( '/[^a-z0-9 _.-]/', '', $string );        
+                // and everything that is still left that hasn't been
+                // replaced/encoded, throw it away
+            $good_characters = "a-z0-9.".$separator;
+            if(!$domainize){
+                    // need double backslash to pass the escape to
+                    // preg_replace on the next line
+                $good_characters .= " _\\-";
+            }
             
+            $string = preg_replace( '/[^'.$good_characters.']/', '', $string );        
+            
                 // remove doubled separators
             $string = preg_replace("/[".$separator."]+/", $separator, $string);
                 // remove starting and trailing separator chars
             $string = trim($string, $separator);
-
+            if($domainize){
+                // remove trailing dots - LT will add them back in if appropriate
+                $string = trim($string, ".");
+            }
+            
             return $string;            
         }
 		
@@ -467,28 +489,7 @@
          */
         function domainize( $string )
         {
-		    // remove unnecessary spaces and make everything lower case
-		    $string = preg_replace( "/ +/", " ", strtolower($string) );
-
-            // removing a set of reserved characters (rfc2396: ; / ? : @ & = + $ ,)
-            $string = str_replace(array(';','/','?',':','@','&','=','+','$',','), '', $string);
-
-            include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
-			$config =& Config::getConfig();
-            $sep = $config->getValue( "urlize_word_separator", URLIZE_WORD_SEPARATOR_DEFAULT );
-
-            // replace some characters to similar ones
-            // underscores aren't allowed in domain names according to rfc specs, and
-            // cause trouble in some browsers, particularly with cookies.
-            $search  = array('-', '_',' ', 'ä','ö','ü','é','è','à','ç','à','è','ì','ò','ù','á','é','í','ó','ú','ë','ï' );
-            $replace = array( $sep, $sep, $sep, 'a','o','u','e','e','a','c','a','e','i','o','u','a','e','i','o','u','e','i' );
-            $string = str_replace($search, $replace, $string);
-
-            // and everything that is still left that hasn't been replaced/encoded, throw it away
-            $string = preg_replace( "/[^a-z0-9.".$sep."]/", '', $string );
-            $string = trim($string, "-.");
-
-            return $string;            
+            return Textfilter::urlize($string, true);
         }
 
 		/**

Modified: plog/branches/lifetype-1.2/class/test/tests/data/textfilter_test.class.php
===================================================================
--- plog/branches/lifetype-1.2/class/test/tests/data/textfilter_test.class.php	2007-04-07 19:13:31 UTC (rev 5284)
+++ plog/branches/lifetype-1.2/class/test/tests/data/textfilter_test.class.php	2007-04-07 20:15:17 UTC (rev 5285)
@@ -60,9 +60,9 @@
 				"test-blog" => "test{$sep}blog",
 				"test_blog" => "test{$sep}blog",
 				"test.blog" => "test.blog",
-				"??test//blog" => "testblog",
+				"??test//blog" => "test{$sep}blog",
 				"==================test blog" => "test{$sep}blog",
-				"this.has.dots_and-hyphens----and   spaces		    " => "this.has.dots{$sep}and{$sep}hyphens{$sep}{$sep}{$sep}{$sep}and{$sep}spaces"
+				"this.has.dots_and-hyphens----and   spaces		    " => "this.has.dots{$sep}and{$sep}hyphens{$sep}and{$sep}spaces"
 			);
 			
 			foreach( $tests as $input => $output ) {



More information about the pLog-svn mailing list