Harea Costea
Harea Costea

Reputation: 273

Preg_replace in php for a specified construction

I have a problem with my preg_replace. I need to do this for a string who contains more @..@, For example I have :

@CN@ This is a test. Big test @DATE@ and @DATE_END@.

Now I want to get a list with :

My code:

$pattern = '/@[_a-zA-Z0-9]*@/';
preg_match_all($pattern,'',$aData[$i]['tags']);
@CN@,@DATE@,@DATE_END@

Between @...@ can be multiple expressions.

Upvotes: 0

Views: 50

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

Use preg_match_all with implode function.

$str = "@CN@ This is a test. Big test @DATE@ and @DATE_END@.";
preg_match_all('/@[^@]*@/', $str, $match);
echo implode(",", $match[0]);

The above implode function helps to join the array elements with comma as delimiter.

Output:

@CN@,@DATE@,@DATE_END@

Upvotes: 1

Toto
Toto

Reputation: 91385

I guess you want to use preg_match_all instead:

preg_match_all('/(@[^@]+@)/', $input, $match)

Upvotes: 0

Related Questions