PHP minus symbol in URL (rawurlencode vs urlencode)

3.2k views Asked by At

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

4

There are 4 answers

0
Entity On BEST ANSWER

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.

0
Ewan Heming On

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);
0
slifty On

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.

0
groovehunter On

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