将URL用户中的字符+替换为-。

问题描述 投票:0回答:1

我想换掉 + 在用户名下有 -.+ 当用户名中包含一个空格时,会自动创建,如 john taylor &gt.我找到了这些功能,但是无法解决这个问题(我用的是插件会员--终极会员,团队支持没有回答)。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'");
}


php wordpress function url str-replace
1个回答
0
投票

你可以用-替换字符串中的+。

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

0
投票

使用 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);

-1
投票

首先要确定你能得到什么?$_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'");
}
© www.soinside.com 2019 - 2024. All rights reserved.