我正在使用一个函数来显示标题标签上的URL,我对这个函数进行了编码,它可以正常工作,但我想对它进行进一步的自定义。
function title_tag()
{
$a = $_SERVER[\'REQUEST_URI\'];
$b = strtoupper($a);
$c = str_replace(\'-\', \' \', $b);
$d = str_replace(\'/\', \' - \', $c);
$e = substr($d, 2);
return $e;
}
在标题标签中插入此代码后,此函数将显示标题上的url。
<title><?php echo title_tag(); ?></title>
当前代码以这种格式显示标题,我的网站标题:
STANFORD UNIVERSITY - MBA IN CALIFORNIA - MBA -
但我想以这种格式显示,只是标点符号的变化
STANFORD UNIVERSITY - MBA IN CALIFORNIA, MBA -
中间只有一个逗号作为分隔符我怎么能有这个?
最合适的回答,由SO网友:petermolnar 整理而成
function title_tag () {
// 0. uppercase string
$str = strtoupper ( $_SERVER[\'REQUEST_URI\'] );
// 1. remove trailing and init slash
$str = trim ( $str , \'/\' );
// 2. add search and replace chars;
// two array, with same element size,
// 1. element of search array will be replaced
// with the first element of replace array
$search = array (
\'-\',
\'/\'
);
$replace = array (
\' \',
\' - \'
);
// 3. replace the chars
$str = str_replace( $search , $replace , $str );
// 4. replace the last occurance of - for ,
// $pos finds the position of the last occurance
// and fortunately, PHP strings can be manipulated
// as arrays, so replace the array element with the
// character
$pos = strrpos ( $str , \'-\' );
$str{$pos} = \',\';
// you\'re ready
return $str;
}