PHP将html表格转化为数组

	/**
     * 将html表格转化为数组
     * @param $table Html表格字符串
     * @return mixed 数组
     */
    public function get_td_array($table) {

        $table = preg_replace("']*?>'si","",$table);

        $table = preg_replace("']*?>'si","",$table);

        $table = preg_replace("']*?>'si","",$table);

        $table = str_replace("","{tr}",$table);

        $table = str_replace("","{td}",$table);

        //去掉 HTML 标记

        $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table);

        //去掉空白字符

        $table = preg_replace("'([rn])[s]+'","",$table);

        $table = preg_replace('/ /',"",$table);

        $table = str_replace(" ","",$table);

        $table = str_replace("\r\n","",$table);

        $table = explode('{tr}', $table);

        array_pop($table);

        $td_array = [];
        foreach ($table as $key=>$tr) {

            $td = explode('{td}', $tr);

            array_pop($td);

            foreach ( $td as $k => $v ) {
                $td[$k] = trim( $v );
            }

            $td_array[] = $td;
        }


        return $td_array;
    }

你可能感兴趣的:(编程语言,格式转换)