2

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.

3 Answers 3

8

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.