title tag function

时间:2011-03-26 作者:Niraj Chauhan

我正在使用一个函数来显示标题标签上的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 -

中间只有一个逗号作为分隔符我怎么能有这个?

1 个回复
最合适的回答,由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;
}
结束

相关推荐

高级集成-WordPress+YOURLS

我一直在使用自定义YOURLS 系统为我的博客网络创建我自己的URL缩写器。一切都很好,只有少数例外(bug已经报告给开发人员)。但我的目标是实现现有WordPress YOURLS插件中未包含的更复杂的功能。目前,当发布帖子时,WordPress会自动登录到您的LS,创建一个简短的URL,并将通知推送到Twitter。它运行良好,可以为我跟踪所有内容(短URL由WordPress记忆并插入适当的<meta> 标签)。我想做的是,我把我的博客帖子安排在太平洋时间上午8点。如果你早上8点在推特上