0

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

3
  • What's the expected output ? Commented Jul 26, 2013 at 14:06
  • the output should be the whole string (i.e. html code) without the placeholders, including the delimiters '#' Commented Jul 26, 2013 at 14:17
  • Edit your question with actual input and expected output. A slight change in a regex might cause it to not work. Commented Jul 26, 2013 at 14:35

2 Answers 2

1
echo preg_replace('/#\w+#/', '', $text);
Sign up to request clarification or add additional context in comments.

1 Comment

\w == [a-zA-Z0-9_] so you can remove that underscore
1
$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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.