Wednesday, January 20, 2010

PHP Tips

PHP String Functions
http://www.w3schools.com/PHP/php_ref_string.asp

1.  Contain String

int strpos ( string $haystack, mixed $needle [, int $offset = 0 ])  - Find position of first occurrence of a string

int strstr ( string $haystack, mixed $needle [, bool $before_needle = false ]) Find first occurrence of a string

2. Array
array_merge( array1, array2 ...)  - merge array
array_intersect(array1,array2,array3...) - compares array values, and returns the matches
array_diff(array1,array2,array3...)  - compares array values, and returns the difference

array_unique(array) - remove duplicate value
in_array(search,array,type) - searches an array for a specific value (true or false)

Remove an item from array base on the value

foreach($myArray as $key => $item) {
   if($item->getValue() == $remove_value) {
      unset($myArray[$key]);
   }
}


explode — Split a string by string
implode — Join array elements with a string

array explode ( string $delimiter , string $string [, int $limit ] )
string implode ( string $glue , array $pieces )


3. Input & Output
$user_input = $this->getRequestParameter('query');
$user_input = htmlentities($user_input, ENT_QUOTES, 'UTF-8'); 

$output = html_entity_decode($value, ENT_QUOTES, 'UTF-8');

Test

Iñtërnâtiônàlizætiøn
®™©&'/"\"¤¶>%?


$name = html_entity_decode($name, ENT_QUOTES, 'UTF-8');
$name = truncate_text($name, 40, '..');
$name= htmlentities($name, ENT_QUOTES, 'UTF-8');

4.php.ini

phpinfo();

such as /etc/php.ini

No comments:

Post a Comment