0

I need to remove

[PRODUCT ID=123]

from text

Some random dummy [PRODUCT ID=123] text just for example [PRODUCT ID=321]

Where ID (after the = sign) can be any number (integer).

I've tried with PHP

preg_match_all("|[[^]]+](.*)[]/[^]]+]|U", $text, $out);

but it doesn't work as I thought it will so I'm trying with regex but currently I'm lost and I need your help.

Thank you.

1
  • 1
    do you need to remove just bracketed PRODUCT ID text, or ANYTHING in [] pairs? Commented Apr 16, 2013 at 18:49

3 Answers 3

2
preg_replace("/\[PRODUCT ID=(\d*)\]/", '', 'Some random dummy [PRODUCT ID=123] text just for example [PRODUCT ID=321]');
Sign up to request clarification or add additional context in comments.

1 Comment

Or, if you want a generic version: \[(?:\w+\s\w+=\d+)\]
0

Try this :

$str = 'Some random dummy [PRODUCT ID=123]text just for example [PRODUCT ID=321]';
preg_replace('/\[PRODUCT ID=.*?\]/', '', $str);

Hope this helps :)

Comments

0
$a = ' ads [PRODUCT ID=123] [PRODUCT ID=4124124321] asd \ a sda s';
$a = preg_replace("~(\[PRODUCT ID=)([0-9]*)(\])~", "", $a);
echo $a;
// ads asd \ a sda s

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.