Msmit1993
Msmit1993

Reputation: 1710

Regular Expression replacing dash with space

I have a problem how do i replace a single dash(-) with a single space.

I tried the following

$test = TEST-test;
preg_replace('\-', '/s', $test);
echo $test;

but no result.

thx,

Upvotes: 4

Views: 3723

Answers (4)

sleeping_dragon
sleeping_dragon

Reputation: 711

use sed

echo $test | sed 's/-/ /g'

Upvotes: 0

jbrtrnd
jbrtrnd

Reputation: 3833

One regular expression to replace a - by a blank :

preg_replace('/-/',' ',$string);

You miss the regular expression delimiters.

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173572

Behold the strtr:

$test = strtr($test, '-', ' ');

Btw, your original code has TEST-test, that needs to be wrapped in quotes:

$test = 'TEST-test';

Upvotes: 10

user1432124
user1432124

Reputation:

Use php str_replace() for this thing

$test = 'TEST-test';
$test=str_replace('-', ' ', $test);
echo $test;

Upvotes: 0

Related Questions