将codeigniter数据传递给简单的html dom url变量

时间:2015-07-02 12:14:32

标签: php mysql codeigniter parsing simple-html-dom

我有问题。我想在一个简单的HTML-DOM URL变量上传递我的Codeigniter Mysql变量(我可以在模板中使用{{ category:country }}使用此库http://simplehtmldom.sourceforge.net/)。没有结果我尝试了很多变化。

这是我的代码的开头:

<?php

include_once('simple_html_dom.php');

ini_set('display_errors', true);
error_reporting(E_ALL);


$url = 'http://address.com/".{{ category:country }}."/'; 

?>

我想打开地址,就像我得到德国变量一样,然后解析:http://address.com/germany

非常感谢!

2 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,那么你想要的可能是棘手的或不可能的。

基本上,简单的HTML DOM正在使用内存中的代码,因此当您提供$url时,它会尝试访问名为http://address.com/".{{ category:country }}."/的网址

模板解析器作用于PHP的输出缓冲区,因此在它准备好调用所有ob_* functions

之前不会调用它

如果您的模板引擎支持它,那么您将需要执行以下操作:

<?php

include_once('simple_html_dom.php');

ini_set('display_errors', true);
error_reporting(E_ALL);

$url = 'http://address.com/".{{ category:country }}."/'; 

// Find a function which will parse the string you have passed into it
$url = FAKE_FUNCTION_templating_engine_PARSE_NOW($url);

// At this point, Simple HTML DOM will be able to properly use the $url

?>

答案 1 :(得分:0)

但是在pyrocms中有一个内置的解析器:

以下是代码:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * CodeIgniter Dwoo Parser Class
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    Parser
 * @license  http://philsturgeon.co.uk/code/dbad-license
 * @link        http://philsturgeon.co.uk/code/codeigniter-dwoo
 */

class MY_Parser extends CI_Parser {

    private $_ci;

    public function __construct($config = array())
    {
        $this->_ci = & get_instance();

        if ( ! class_exists('Lex_Autoloader'))
        {
            include APPPATH.'/libraries/Lex/Autoloader.php';
        }
    }

    // --------------------------------------------------------------------

    /**
     *  Parse a view file
     *
     * Parses pseudo-variables contained in the specified template,
     * replacing them with the data in the second param
     *
     * @access  public
     * @param   string
     * @param   array
     * @param   bool
     * @return  string
     */
    public function parse($template, $data = array(), $return = false, $is_include = false, $streams_parse = array())
    {
        $string = $this->_ci->load->view($template, $data, true);

        return $this->_parse($string, $data, $return, $is_include, $streams_parse);
    }

    // --------------------------------------------------------------------

    /**
     *  String parse
     *
     * Parses pseudo-variables contained in the string content,
     * replacing them with the data in the second param
     *
     * @access  public
     * @param   string
     * @param   array
     * @param   bool
     * @return  string
     */
    public function parse_string($string, $data = array(), $return = false, $is_include = false, $streams_parse = array())
    {
        return $this->_parse($string, $data, $return, $is_include, $streams_parse);
    }

    // --------------------------------------------------------------------

    /**
     *  Parse
     *
     * Parses pseudo-variables contained in the specified template,
     * replacing them with the data in the second param
     *
     * @access  protected
     * @param   string
     * @param   array
     * @param   bool
     * @return  string
     */
    protected function _parse($string, $data, $return = false, $is_include = false, $streams_parse = array())
    {
        // Start benchmark
        $this->_ci->benchmark->mark('parse_start');

        // Convert from object to array
        is_array($data) or $data = (array) $data;

        $data = array_merge($data, $this->_ci->load->_ci_cached_vars);

        Lex_Autoloader::register();

        if ($streams_parse and isset($streams_parse['stream']) and isset($streams_parse['namespace']))
        {
            // In some very rare cases (mainly in the pages module), we need to
            // change the field that is being passed to plugin_override() as row_id.
            // This is where that happens.
            $id_name = (isset($streams_parse['id_name']) and $streams_parse['id_name']) ? $streams_parse['id_name'] : 'id';

            $this->_ci->load->driver('Streams');
            $parsed = $this->_ci->streams->parse->parse_tag_content($string, $data, $streams_parse['stream'], $streams_parse['namespace'], false, null, $id_name);
        }
        else
        {
            $parser = new Lex_Parser();
            $parser->scope_glue(':');
            $parser->cumulative_noparse(true);
            $parsed = $parser->parse($string, $data, array($this, 'parser_callback'));
        }

        // Finish benchmark
        $this->_ci->benchmark->mark('parse_end');

        // Return results or not ?
        if ( ! $return)
        {
            $this->_ci->output->append_output($parsed);
            return;
        }

        return $parsed;
    }

    // --------------------------------------------------------------------

    /**
     * Callback from template parser
     *
     * @param   array
     * @return   mixed
     */
    public function parser_callback($plugin, $attributes, $content)
    {
        $this->_ci->load->library('plugins');

        $return_data = $this->_ci->plugins->locate($plugin, $attributes, $content);

        if (is_array($return_data) && $return_data)
        {
            if ( ! $this->_is_multi($return_data))
            {
                $return_data = $this->_make_multi($return_data);
            }

            // $content = $data['content']; # TODO What was this doing other than throw warnings in 2.0?
            $parsed_return = '';

            $parser = new Lex_Parser();
            $parser->scope_glue(':');

            foreach ($return_data as $result)
            {
                // if ($data['skip_content'])
                // {
                //  $simpletags->set_skip_content($data['skip_content']);
                // }

                $parsed_return .= $parser->parse($content, $result, array($this, 'parser_callback'));
            }

            unset($parser);

            $return_data = $parsed_return;
        }

        return $return_data ? $return_data : null;
    }

    // ------------------------------------------------------------------------

    /**
     * Ensure we have a multi array
     *
     * @param   array
     * @return   int
     */
    private function _is_multi($array)
    {
        return (count($array) != count($array, 1));
    }

    // --------------------------------------------------------------------

    /**
     * Forces a standard array in multidimensional.
     *
     * @param   array
     * @param   int     Used for recursion
     * @return  array   The multi array
     */
    private function _make_multi($flat, $i=0)
    {
        $multi = array();
        $return = array();
        foreach ($flat as $item => $value)
        {
            if (is_object($value))
            {
                $return[$item] = (array) $value;
            }
            else
            {
                $return[$i][$item] = $value;
            }
        }
        return $return;
    }
}

// END MY_Parser Class

/* End of file MY_Parser.php */