MarcoSpada
MarcoSpada

Reputation: 53

PHP remove delimited placeholders from string

I'm trying to remove some chunks from a string, which is basically html code; the chunks are delimited by # I'm a zero with regexp, but found this method on another SO topic (Replace everything between and including two characters using regex in php) but it's not working and I'm not able to get why. My chunks are placeholders inside html code, like this :

#TEXT_2#

and I'tried with these functions (none of them succeeded):

$text = preg_replace('/\[[#]]*]/', '', $text);
$text = preg_replace("/\\#\\\(\d).*?\\#/", "", $text);
$text = preg_replace( '~\#(\d+)\#~' , "", $text);

Can anybody suggest me a way to do it ? Any help would be higly appreciated. Thx

Upvotes: 0

Views: 663

Answers (2)

Expedito
Expedito

Reputation: 7795

echo preg_replace('/#\w+#/', '', $text);

Upvotes: 1

Ioannis Lalopoulos
Ioannis Lalopoulos

Reputation: 1511

$text = "#in1#out1#in2#out2#in3###out3";
$text = preg_replace('/#[^#]*#/', '', $text);
echo $text, PHP_EOL;

it should echo only the out parts i.e.: out1out2out3

Upvotes: 1

Related Questions