Benjamin
Benjamin

Reputation: 2123

Php regexp "deleting only one charactered words" from string

I have a string, "Hello there I have a question"

I want to delete all one charactered words from this string. It must look like "Hello there have question"

I've done this:

$yt_tags = preg_replace('/[\w]{1}/','',$yt_tags);

But it deletes everything in the string.

Thanks for your help.

Upvotes: 2

Views: 234

Answers (3)

mmdemirbas
mmdemirbas

Reputation: 9158

Short answer, you should use \b\w\b.


Regular expression[\w]{1} matches any word character (letter, digit or underscore), but you want to get matches 'in a word boundary'. These are completely different things.

Our test string: Hello there have question

What is your mistake?

\w{1} finds any single word-character anywhere: H e l l o t h e r e h a v e q u e s t i o n

\w{2} finds word-character pairs by order: He ll th er ha ve qu es ti on Note that odd-length words missed last letter (Hello --> He ll)

\w{3} finds word-character triplets anywhere: Hel the hav que sti

etc...

And what we need?

\b...\b finds matches in a word boundary.

\w finds a word-characters (digit, letter or underscore_).

\b\w\b finds one-length words. This is what we need.

HTH

Upvotes: 0

deed02392
deed02392

Reputation: 5022

Possibly quicker but a little more complex could be:

$words = explode(' ', $input_string);
foreach($words as &$w)
{
    if(strlen($w) === 1)
    {
        unset($w);
    }
}

Note this assumes there would only be one character space between every word, which may not be the case. If you know this varies, e.g. it's coming from a form, use regex.

Upvotes: 0

Wrikken
Wrikken

Reputation: 70510

Use a word-boundary (\b):

preg_replace('/\b\w\b\s*/'...

I added in the \s* to 'trim' unneeded extra whitespace (word<space>a<space>word would otherwise become: word<space><space>word instead of word<space>word).

Upvotes: 8

Related Questions