예제 #1
0
    /**
     * This function cuts out requested tag information from html head
     * @access  public
     * @param   $text  html text
     * @param   $tags  a string or an array of requested tags
     * @author  Cindy Qi Li
     */
    public static function getHtmlHeadByTag($text, $tags)
    {
        $head = ContentUtility::getHtmlHead($text);
        $rtn_text = "";
        if (!is_array($tags) && strlen(trim($tags)) > 0) {
            $tags = array(trim($tags));
        }
        foreach ($tags as $tag) {
            $tag = strtolower($tag);
            /* strip everything before <{tag}> */
            $start_pos = stripos($head, '<' . $tag);
            $temp_head = $head;
            while ($start_pos !== false) {
                $temp_text = substr($temp_head, $start_pos);
                /* strip everything after </{tag}> or />*/
                $end_pos = stripos($temp_text, '</' . $tag . '>');
                if ($end_pos !== false) {
                    $end_pos += strlen('</' . $tag . '>');
                    // add an empty line after each tag information
                    $rtn_text .= trim(substr($temp_text, 0, $end_pos)) . '
		
	';
                } else {
                    $end_pos = stripos($temp_text, '/>');
                    if ($end_pos === false && stripos($temp_text, $tag . '>') === false) {
                        //if /> is not found, then this is not a valid XHTML
                        //text iff it's not tag>
                        $end_pos = stripos($temp_text, '>');
                        $end_pos += strlen('>');
                    } else {
                        $end_pos += strlen('/>');
                    }
                    // add an empty line after each tag information
                    $rtn_text .= trim(substr($temp_text, 0, $end_pos)) . '
		
	';
                }
                // initialize vars for next round of matching
                $temp_head = substr($temp_text, $end_pos);
                $start_pos = stripos($temp_head, '<' . $tag);
            }
        }
        return $rtn_text;
    }