-1

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

benhowdle89
  • 36,900
  • 69
  • 202
  • 331

4 Answers4

1

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.

Entity
  • 7,972
  • 21
  • 79
  • 122
0

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);
Ewan Heming
  • 4,628
  • 2
  • 21
  • 20
0

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.

slifty
  • 13,062
  • 13
  • 71
  • 109
0

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

groovehunter
  • 3,050
  • 7
  • 26
  • 38