📄 08c05-1.php
字号:
<?php// A function to detect absolute paths:function is_absolute($path) { // Check if it begins with a '/', if so, it is an absolute path if ($path{0} == '/') { return true; } else { return false; }}// A function to detect full/absolute URLsfunction is_absoluteurl($path) { // Check if it begins with a protocol specification: if (preg_match('|^[a-zA-Z]+://|', $path)) { return true; } else { return false; }}// A function to detect relative paths:function is_relative($path) { // In this case, if it is neither of the absolute options, then // it is relative: if ( !(is_absolute($path)) && !(is_absoluteurl($path)) ) { return true; } else { return false; }}// Now, a function that given any path, will turn it into an // absolute url and return it:function ensure_absoluteurl($path) { // If this already is an absolute URL, just return it: if (is_absoluteurl($path)) { return $path; } // Else if it is an absolute path, add the http & host to it: elseif (is_absolute($path)) { return "http://{$_SERVER['HTTP_HOST']}{$path}"; } // Finally it is relative, so we need to build the entire string: else { return 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/' . $path; }}// Create some test cases:$tests = array('http://eliw.com/docs/book/php5/01.php', '/docs/book/php5/01.php', 'php5/01.php', '../book/php5/01.php');// For for each case, run all the functions on it:foreach ($tests as $tc) { echo "<p>\n"; echo "Is {$tc} relative? ", is_relative($tc) ? 'Yes' : 'No', "<br>\n"; echo "Is {$tc} absolute? ", is_absolute($tc) ? 'Yes' : 'No', "<br>\n"; echo "Is {$tc} an absolute URL? ", is_absoluteurl($tc) ? 'Yes' : 'No', "<br>\n"; echo "Converted to an absolute URL we get: ", ensure_absoluteurl($tc); echo "</p>\n";}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -