Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8111053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:58:04+00:00 2026-06-06T01:58:04+00:00

Possible Duplicate: Regular expression: match all words except I need your help for using

  • 0

Possible Duplicate:
Regular expression: match all words except

I need your help for using Regex in PHP to negate a selection. So I have a string like this :
“Hello my name is tom”

What I need to do is to delete everything from this string witch is not “tom” or “jack” or “alex” so I tried :

$MyString = "Hello my name is tom"
print_r(preg_replace('#^tom|^jack|^alex#i', '', $MyString));

But it’s not working…

Can you help me with that ?
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:58:05+00:00Added an answer on June 6, 2026 at 1:58 am

    If you want to delete everything except something, may be it’s better done the other way around: capture the something only? For example…

    $testString = 'Hello my name is tom or jack';
    $matches = array();
    preg_match_all('/\b(tom|jack|alex)\b/i', $testString, $matches);
    $result = implode('', $matches[0]);
    echo $result; // tomjack
    

    What you’ve tried to do is use a character class syntax ([^s] will match any character but s). But this doesn’t work with series of characters, there’s no such thing as ‘word class’. )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Regular expression to match hostname or IP Address? I need to validate
Possible Duplicate: php regular expression help? hi, i want to replace i like apple
Possible Duplicate: Using regular expression within a stored procedure I need to validate a
Possible Duplicate: Regex to match URL I wrote a regular expression to validate only
Possible Duplicate: Validating an IP with regex I need the regular expression for finding
Possible Duplicate: Regex to match URL Is there a regular expression to return a
Possible Duplicate: RegEx match open tags except XHTML self-contained tags I have this list
Possible Duplicate: RegEx match open tags except XHTML self-contained tags What is the fastest
Possible Duplicate: Need a Regular expression to extract 5th to 8th charactors in a
Possible Duplicates: Checking for a valid url using Javascript Regular Expressions PHP validation/regex for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.