Codes‎ > ‎

PHP


Unicode URL decode

posted Oct 29, 2009 3:42 AM by Khoi Le   [ updated Oct 29, 2009 3:57 AM ]


These 2 codes fragments have not been tested yet. Posted here for my convenience and yours if you find it useful.


The following code is not mine, the author is PsychoCoder
Please go here to see his post
http://www.dreamincode.net/code/snippet1822.htm

Instructions: The urldecode function in PHP does a pretty good job decoding URL's. One shortcoming is with URL's that contain Unicode characters such as %0. This can cause trouble, especially when working with fixed length string.

Just pass the function your URL

Snippet


  1. //Function to decode URL's that contain Unicode characters
  2. function unicode_urldecode($url)
  3. {
  4.   //split the URL into an array
  5.   $url_array = split ("%",$url);
  6.   //Make sure we have an array
  7.   if (is_array($url_array))
  8.   {
  9.     //Loop while the key/value pair of the array
  10.     //match our list items
  11.     while (list ($k,$v) = each ($url_array))
  12.     {
  13.        //use base_convert to convert each character
  14.        $ascii = base_convert ($v,16,10);
  15.        $ret .= chr ($ascii);
  16.     }
  17.  }
  18.  //return the decoded URL
  19.  return ("$ret");
  20. }

Using Regex

The other that I have found in PHP manual contribute by anonymous author
Search for function chr

$url = preg_replace('~%([0-9a-f])([0-9a-f])~ei', 'chr(hexdec("\\1\\2"))', $url);

'M%C3%BCnchen' (which is what a Browser makes of an URL containing verbatim utf-8) gives a nice 2-byte-unicode-char: 'München'



Class members (properties) overloading

posted Oct 20, 2009 4:02 AM by Domain Admin   [ updated Oct 29, 2009 3:59 AM by Khoi Le ]


This class demonstrate the overloading of members in PHP 5.x

class myClass {

    private $values;
    
    public function __construct(){
    
        $this->values = array();
    }
    
    /**
     * Setting values; $this->nm = $val;
     *
     * @param string $nm; comply to variable name rules
     * @param mixed $val
     */
    public function __set($nm, $val){
       
        $this->values[$nm] = $val;
    }
    
    /**
     * Getting values; $this->nm
     *
     * @param string $nm; comply to variable name rules
     * @return mixed
     */
    public function __get($nm){
       
        return isset($this->values[$nm]) ? $this->values[$nm] : null;                
    }
    
    /**
     * Does this value exists or the value is null
     *
     * @param string $nm; comply to variable names rule
     * @return boolean
     */
    public function __isset($nm){
    
        return isset($nm, $this->values);
    }
    
    /**
     * Unset the property
     *
     * @param string $nm;
comply to variable names rule
     */
    public function __unset($nm){
    
        unset($this->values[$nm]);
    }

}

$obj = new myClass();

$obj->name = "John";
$obj->age  = 16;

echo $obj->name;
echo "<br>";
echo $obj->age;
echo "<br>";

/**
 * Output
 * John
 * 16
 */

echo isset($this->age) ? 'yes' : 'no';
/**
 * Output
 * yes
 */

unset($this->age);
echo isset($this->age) ? 'yes' : 'no';
/**
 * Output
 * No
 */


‹ Prev    1-2 of 2    Next ›