HTML and CSS Reference
In-Depth Information
/**
* Determines the controller name using the first element of the URI array
*
* @param $uri_array array The broken up URI
* @return string The controller classname
*/
function get_controller_classname( &$uri_array )
{
$controller = array_shift($uri_array);
return ucfirst($controller);
}
/**
* Removes unwanted double slashes (except in the protocol)
*
* @param $dirty_path string The path to check for unwanted slashes
* @return string The cleaned path
*/
function remove_unwanted_slashes( $dirty_path )
{
return preg_replace('~(?<!:)//~', '/', $dirty_path);
}
Using preg_replace() , this function checks for any occurrences of a double slash ( // ) that isn't preceded by a
colon ( : ) and replaces them with a single slash ( / ).
Because regular expressions can be a little hairy to look at 1 , let's break this one down bit by bit:
~ —The opening delimiter; this simply tells the function that a regex pattern is beginning
(?<!:) —What's called a “negative lookbehind,” which has three main components:
Enclosing parentheses—Define the lookbehind
?<! —The actual lookbehind, which literally tells the regex, “look at the character right
before the one being matched”
: —The expression or character that we don't want to match; in this case, it's the colon
preceding the two slashes, which indicates that it's the protocol and should not be replaced
// —The characters to look for; in this case, the double slashes
~ —The closing delimiter; this tells the function that the regex pattern is ending
Continuing with the previous URI example, the double-slash problem is solved by running the combined URI
parts through remove_unwanted_slashes() :
$services_uri = remove_unwanted_slashes($site_uri . $services_link);
This stores a proper URI in $services_uri :
http://www.example.org/services/
1 RegExiscomplexenoughtobeworthyofitsownbook,whichhasconvenientlybeencompiledonlineat
http://www.regular-expressions.info/
 
 
 
Search WWH ::




Custom Search