Url hyphenated to string in php

Separate hyphenated string to words or titlecase. Merge words into url string with hyphen

Hyphenated url string

A dashed string is a string which is separated by hyphen symbol. Normally it is used in the urls for filenames to join words. It is easier to parse hyphen separated urls or file names than space separated. Urls using hyphenated words over camelCase convention.

For example, the below string can be a filename

hyphen-string-to-title-case

IN PHP, string replace and remove the hyphen character.

$str = str_replace('-',' ',$str)

Output:

hyphen string to title case

String to title case in PHP using ucwords

Next if we wish to convert it into title case we can run ucwords function

ucwords($str)

Convert Hyphen separated string to title case in a single line. Combine both the methods together.

ucwords(str_replace('-',' ',$str))

If you only want to show first letter to uppercase, use the ucfirst() method

Converting words to hyphenated url string

First replace the spaces with hyphen and convert the string to lowercase. Lowercase is preferred as a convention in the url. It avoid ambiguity and thus avoid 404 error.

strtolower(str_replace(' ','-',$str))