PHP/vBulletin-通过函数传递变量并用输出替换变量



我试图通过一个函数传递论坛标题变量,然后用输出替换标题的变量。我已经阅读了文档,并成功地在页面上进行了响应,但当我试图实现我的变量时,我会感到困惑。

我在global_start创建了一个插件,其中包含以下函数:

function seoUrl($string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[s_]/", "-", $string);
}

然后我在forumbit_display上创建了另一个插件:

$san = 'seoUrl';
$san($forum[title]);
$forumname = $san($forum[title]);

然而,我的变量$forumname并没有输出任何内容。

我已经成功地实现了这个精简版本:

$forumname = strtolower(str_replace(' ', '-', $forum[title]));

我只是想通过函数来帮助克服这个学习曲线。为什么我没有看到任何输出?

修改后需要返回字符串。

function seoUrl($string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[s_]/", "-", $string);
return $string; 
}

注意当您传递字符串作为函数参数时,该值的副本将传递给您的函数。因此,在函数中所做的任何修改都不会影响函数外的原始变量,除非您通过引用传递它。

如何获取引用:在参数前面加上&

function seoUrl(&$string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[s_]/", "-", $string);
}

现在,上面的操作将在不返回修改后的字符串的情况下进行。

最新更新