Flipkart

Monday, October 3, 2011

Strip only given Tags PHP

/**
 *
 * @param String $str
 * @param tags which needs to be strip $tags
 * @param boolean $stripContent
 * @return string
 */
function strip_only_tags($str, $tags, $stripContent=false) {
    $content = '';
    if(!is_array($tags)) {
        $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
        if(end($tags) == '') array_pop($tags);
    }
    foreach($tags as $tag) {
        if ($stripContent)
             $content = '(.+</'.$tag.'(>|\s[^>]*>)|)';
         $str = preg_replace('#</?'.$tag.'(>|\s[^>]*>)'.$content.'#is', '', $str);
    }
    return $str;
}

Remove array Element based on key value PHP

/**
 * Remove array Element based on key value
 * @param $arr
 * @param $key
 * @return array
 */
function array_pop_by_key($arr, $key) {
    $array_keys = array_keys($arr);
    foreach($arr as $array_key => $value) {
        if($array_key == $key) {
            unset($arr[$key]);
        }
    }
    return $arr;

}