PHP

Flatten array

? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 public function array_flatten($a, $f = array()) {     if (!$a

Read More »

Highlight matching keyword in a string

public function str_highlight($text, $needle = ”, $options = null, $highlight = null) { $STR_HIGHLIGHT_SIMPLE = 1; $STR_HIGHLIGHT_WHOLEWD = 2; $STR_HIGHLIGHT_CASESENS = 0; $STR_HIGHLIGHT_STRIPLINKS = 8;

Read More »

Add href inside string

public function str2href($str) { $str = preg_replace(‘/s(w+://)(S+)/’, ‘ <a href=”\1\2″ target=”_blank”>\1\2</a>’, $str); }

Read More »

Normalize special characters

public function normaliza($string) { $normalizeChars = array( ‘Š’ => ‘S’, ‘š’ => ‘s’, ‘Ð’ => ‘Dj’, ‘Ž’ => ‘Z’, ‘ž’ => ‘z’, ‘À’ => ‘A’,

Read More »

Check URL without downloading the body

public function url_exists($url) { $handle = curl_init($url); if (false === $handle) { return false; } curl_setopt($handle, CURLOPT_HEADER, false); curl_setopt($handle, CURLOPT_FAILONERROR, true); // this works curl_setopt($handle,

Read More »

XML to PHP array

public function xml2array($contents, $get_attributes = 0) { if (!$contents) { return array(); } if (!function_exists(‘xml_parser_create’)) { //print “‘xml_parser_create()’ function not found!”; return array(); } //Get

Read More »

Image to extension

public function image_type_to_extension($imagetype, $include_dot = false) { if (empty($imagetype)) { return false; } if ($include_dot) { $dot = ‘.’; } else { $dot = ”;

Read More »

Convert PHP array to Bootstrap grid

function bootstrapGrids($gridCols, $columns = 3) { if (is_array($gridCols) && count($gridCols) && is_numeric($columns)) { $array = array_chunk($gridCols, ceil(count($gridCols) / $columns)); $content .= ‘<div class=”row”>’; foreach ($array

Read More »

Convert timestamp to range date string

function getRangeDateString($timestamp) { if ($timestamp) { $currentTime=strtotime(‘today’); // Reset time to 00:00:00 $timestamp=strtotime(date(‘Y-m-d 00:00:00’,$timestamp)); $days=round(($timestamp-$currentTime)/86400); switch($days) { case ‘0’; return ‘Today’; break; case ‘-1’; return

Read More »

Most Popular