Configuration

Like the cURL extension, the tidy extension operates largely on the concept of configuration; hence, $config parameters are present in all calls in the above example. Unlike most other extensions, this parameter can actually be one of two things: an associative array of setting-value pairs or the path to an external configuration file.

The configuration file format is somewhat similar to individual style settings in a CSS stylesheet. An example is shown below. It’s unlikely that a non-developer will need to access the configuration settings and not the PHP source code using tidy as well. As such, separation into an external configuration file is really only useful for the sake of not cluttering source code with settings. Additionally, because the configuration file is read from disk, it may pose performance concerns when in high use.

// single-line comment /* multi-line comment */ indent: false /* setting: value */ wrap: 78

When using the object-oriented API, an alternative to using configuration files is subclassing the tidy class and overriding its parseString and parseFile methods to automatically include specific configuration setting values. This method allows for easy reuse of tidy configurations.

<?php
class mytidy extends tidy { private $_default = array(
'indent' => false,
'wrap' => 78
);

public function parseFile($filename, $config, $encoding, $use_include_path=false) { return parent::parseFile(
$filename,
array_merge($this->_default, $config),
$encoding,
$use_include_path
);
)
public function parseString($filename, $config, $encoding $use_include_path=false) { return parent::parseString(
$filename,
array_merge($this->_default, $config),
$encoding,
$use_include_path
);
}
}
?>
  • array_merge is used to consolidate default parameter values in the $_default property into the specified $config array. Any parameters specified in the original $config array will take precedence over corresponding parameters specified in $_default.
  • parseFile and parseString pass the modified $config parameter with all other provided parameters to their respective methods in the tidy class and return the resulting return value.

© Tidy Extension — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (01.09.2014)
Views: 369 | Rating: 0.0/0
Total comments: 0
avatar