我想换掉 +
在用户名下有 -
.+
当用户名中包含一个空格时,会自动创建,如 john taylor
>.我找到了这些功能,但是无法解决这个问题(我用的是插件会员--终极会员,团队支持没有回答)。john+taylor
我发现了这些功能,但无法解决这个问题(我使用的是插件会员--终极会员,团队支持没有回答).有人能帮帮我吗?
function myUrlEncode($string) {
$entities = array(' ');
$replacements = array('-');
return str_replace($entities, $replacements, urlencode($string));
}
function str_replace_url($url)
{
str_replace(" ", "-", $url);
}
function try () {
$user = str_replace(' ', '-', $_GET['user']);
$user = mysql_escape_string($user);
$query = mysql_query("SELECT * FROM users WHERE username = 'user'");
}
你可以用-替换字符串中的+。
function plus_to_minus($user) {
return str_replace("+", "-", $user);
}
echo plus_to_minus("john+taylor");
// output: john-taylor
echo plus_to_minus("john++taylor");
// output: john--taylor
使用 preg_replace
如下图所示。
$string = 'I love to work on the PHP development';
$slug = preg_replace('/\s/', '+', $string);
echo $slug;
要转换为函数
function convertToUrl($string)
{
return preg_replace('/\s/i', '+', $string);
}
//usage
echo convertToUrl(string);
首先要确定你能得到什么?$_GET['user']
用php var_dump
函数,然后使用 str_replace
根据你得到的字符串。
$user = $_GET['user'];
// Verify $user contains +
var_dump($user);
// Replace + with -
$user = str_replace("+", "-", $user);
// Verify $user is correct
var_dump($user);
然后调用你的 try()
函数,但要注意查询字符串中的$user变量,你省略了 $
.
function try () {
$user = mysql_escape_string($user);
// Here must be $user and not user or you will search for 'user' username
$query = mysql_query("SELECT * FROM users WHERE username = '$user'");
}