quokka
quokka

Reputation: 313

regular expression to use in PHP for removing particular text in brackets from a text string

Can't figure this one out... I need a regular expression to use in PHP for removing particular text from a text string...

My text strings are like this: "The product we're talking about [Art.0430000] is a good product" "The product we're talking about [Art.0430001] is a good product" "The product we're talking about [Art.7852000] is a good product"

I need to remove [Art.0430000], [Art.0430001], [Art.7852000] from the strings...

Upvotes: 0

Views: 74

Answers (4)

Jacob Eggers
Jacob Eggers

Reputation: 9322

If you also want to compress the white space left over (2 spaces to 1 space);

$pattern = '/\[Art\.[0-9]+\]\s*/';
$result = preg_replace($pattern, "", $string);

Upvotes: 0

marchaos
marchaos

Reputation: 3444

I haven't tested this, but it should work:

preg_replace_all("/\[Art(^\]+)\]/", "", $string);

Upvotes: 0

WordsWorth
WordsWorth

Reputation: 872

$pattern = "#\[Art\.([o-9])+\]#"; str_replace($pattern,"",$str);

Upvotes: -1

Kerrek SB
Kerrek SB

Reputation: 476960

The regular expression should be:

\[Art\.[0-9]+\]

(In words: The literal string "[Art.", followed by one or more numerals in the range 0-9, followed by the literal string "]".)

In PHP:

$pattern = '/\[Art\.[0-9]+\]/';
$result = preg_replace($pattern, "", $string);

Upvotes: 2

Related Questions