Ste
Ste

Reputation: 1520

Remove all hash symbols in a string

I have a string like:

#05#, #45#, #785# ...etc

I need to remove the hash symbols from either side of each id value so that #01544# becomes 01544.

Upvotes: 1

Views: 109

Answers (5)

Chris Eberle
Chris Eberle

Reputation: 48795

Use this:

preg_replace('(#([0-9]+)#)', '\1', '#05#, #45#, #785#');

Upvotes: 2

jeroen
jeroen

Reputation: 91792

You can just use str_replace:

$id = str_replace("#", "", $your_variable);

Upvotes: 2

Rafe Kettler
Rafe Kettler

Reputation: 76985

Why not just use str_replace? E.g.

$string = str_replace('#', '', $string);

For simple patterns like this, regex is never necessary.

Upvotes: 2

Adnan
Adnan

Reputation: 26360

echo str_replace("#", "", "#05#, #45#, #785#");

more info at:

http://php.net/manual/en/function.str-replace.php

Upvotes: 6

Yoshi
Yoshi

Reputation: 54659

Try

$str = trim('#01544#', '#');

Upvotes: 2

Related Questions