PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

URI Validation

The Zend_Uri::check() method can only be used if validation of an existing URI is needed.

Example 986. URI Validation with Zend_Uri::check()

<?php
// Validate whether a given URI is well formed
$valid Zend_Uri::check('http://uri.in.question');

// $valid is TRUE for a valid URI, or FALSE otherwise.

Zend_Uri::check() returns a boolean, which is more convenient than using Zend_Uri::factory() and catching the exception.

Allowing "Unwise" characters in URIs

By default, Zend_Uri will not accept the following characters: "{", "}", "|", "\", "^", "`". These characters are defined by the RFC as "unwise" and invalid; however, many implementations do accept these characters as valid.

Zend_Uri can be set to accept these "unwise" characters by setting the 'allow_unwise' option to boolean TRUE using Zend_Uri::setConfig():

Example 987. Allowing special characters in URIs

<?php
// Contains '|' symbol
// Normally, this would return false:
$valid Zend_Uri::check('http://example.com/?q=this|that');

// However, you can allow "unwise" characters
Zend_Uri::setConfig(array('allow_unwise' => true));

// will return 'true'
$valid Zend_Uri::check('http://example.com/?q=this|that');

// Reset the 'allow_unwise' value to the default FALSE
Zend_Uri::setConfig(array('allow_unwise' => false));

Note

Zend_Uri::setConfig() sets configuration options globally. It is recommended to reset the 'allow_unwise' option to 'FALSE', like in the example above, unless you are certain you want to always allow unwise characters globally.

Zend Framework