preg_replace words with @

Trying to use preg_replace to find words with @ in them and replace the whole word with nothing.

<?php

$text = "This is a text @removethis little more text";

$textreplaced = preg_replace('/@*. /', '', $text);

echo $captions;

Should output: This is a text little more text

Been trying to google on special charc and such but am lost.

Upvotes: 0

Views: 71

Answers (2)

Christian Pavilonis
Christian Pavilonis

Reputation: 92

Only finding one at character at a time

I believe you are only finding the '@' to begin with, but if you find the whole string inside use \b around the regex so your final regex should be something like /(@).{2,}?\b/.

The ? mark is important because regexes are greedy and grab as many letters as posible

Just a tip visit a tester like regexpal

Upvotes: 1

Federkun
Federkun

Reputation: 36924

Use \w:

$textreplaced = preg_replace('/@[\w]+ /', '', $text);
echo $textreplaced;

Upvotes: 1

Related Questions