PHP URL 中的减号(rawurlencode 与 urlencode)

发布于 2024-10-19 18:23:27 字数 132 浏览 3 评论 0原文

我尝试过使用 rawurlencode 和 urlencode,两者都给我“&”和另一个“+”。

我要问的是,是否有一个 PHP 函数可以在单词之间添加“-”,就像 Stack Overflow 上那样?

谢谢

I've tried using rawurlencode and urlencode, both give me "&" and the other "+".

What i'm asking is, is there a PHP function that puts "-" in between words, like on Stack Overflow?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光清浅 2024-10-26 18:23:27

如果这就是您想要做的,您可以使用:

    $url = str_replace(" ", "-", $url);

然后使用 urlencode 对其进行编码。 EG:

    function myurlencode($url)
    {
        return urlencode(str_replace(" ", "-", $url));
    }

编辑

并根据PHP手册,它替换除 -_ 之外的所有非字母数字字符。百分号 (%) 后跟两个十六进制数字和编码为加号 (+) 的空格。

If thats all you're trying to do, you could just use:

    $url = str_replace(" ", "-", $url);

And then use urlencode to encode it after that. E.G.:

    function myurlencode($url)
    {
        return urlencode(str_replace(" ", "-", $url));
    }

EDIT

And according to the PHP manual, it replaces all non-alphanumeric characters except -_. with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.

極樂鬼 2024-10-26 18:23:27

rawurlencodeurlencode 用于使 URL 的格式正确。如果你想用破折号替换空格,你需要使用 str_replace

str_replace(" ", "-", $url);

rawurlencode and urlencode are for making URL's the correct format. If you want to replace spaces for dashes you need to use str_replace:

str_replace(" ", "-", $url);
方觉久 2024-10-26 18:23:27

如果您只想将一类字符替换为另一类字符,请使用 str_replace< /a>

例如:

$newURL = str_replace(" ", "-", $oldURL);

会做你想做的

urlencode 是非常具体的字符替换集,遵循 url 编码标准。

If you just want to replace a class of characters with another, use str_replace

For example:

$newURL = str_replace(" ", "-", $oldURL);

would do what you want

urlencode is a very specific set of character replacements, which follow the standard for url encoding.

迷鸟归林 2024-10-26 18:23:27

好的,正如前面提到的,url_encode 与从文本生成 URL 无关,即使它们对 SEO 友好。通常这称为 slugify url。我想这通常就是您正在寻找的,对吧?

这是这种方法的示例 http://sourcecookbook .com/en/recipes/8/function-to-slugify-strings-in-php

Okay, as mentioned url_encode is independent from making URLs from text ie to make them SEO-friendly. Usually this is called slugify an url. I guess this is generally what you're looking for, right?

Here is an example of such a method http://sourcecookbook.com/en/recipes/8/function-to-slugify-strings-in-php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文