dluna88
dluna88

Reputation: 13

Search and replace words in a string with regular expression

I have a string like this:

"This is a random string with @[certain] words with this format @[something]"

I want to replace that words wrapped by @[]

So my result has to be like this:

"This is a random string with words with the format"

I'm using PHP.

Upvotes: 0

Views: 51

Answers (1)

Venkata Krishna
Venkata Krishna

Reputation: 1776

You can make use of regular expressions for the pattern and replace your string using preg_replace.

Something like this,

$str = "This is a random string with @[certain] words with this format @[something]";
$newStr = preg_replace('/\ @\[(\w+)\]/','',$str);

You can also write this in a single line,

echo preg_replace('/\ @\[(\w+)\]/','',"This is a random string with @[certain] words with this format @[something]");

Upvotes: 1

Related Questions