webmasters
webmasters

Reputation: 5831

Strip out "-" from variable

I have a variable which has this structure:

$catname = 'this-is-my-category';

How can I strip the "-" and get:

$catname = 'this is my category';

Upvotes: 2

Views: 1822

Answers (4)

Kirby Todd
Kirby Todd

Reputation: 11546

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

Upvotes: 0

mario
mario

Reputation: 145472

For character-wise replacing the ideal function is strtr:

$catname = strtr($catname, "-", " ");

This makes less effort if you later decide to replace e.g. underscores too.

Upvotes: 1

Jason Swett
Jason Swett

Reputation: 45074

$catname = str_replace('-', ' ', $catname);

Upvotes: 1

theomega
theomega

Reputation: 32031

Use the str_replace function like this:

$catname = str_replace('-', ' ', $catname);

This would replace every occurence of - with in your string.

Upvotes: 6

Related Questions