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

  • SEARCH
  • Home
  • 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 8132221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:17:34+00:00 2026-06-06T09:17:34+00:00

I want to find all the demo words using PHP and regEx. $input =’demo’;

  • 0

I want to find all the demo words using PHP and regEx.

$input ='demo';
$pattern = ''; //$input can be used along with regex, please help here 
$text = 'This is dem*#o text, contains de12mo3 text, is .demo23* text' 

if(preg_match($pattern, $text))
{
 echo 'found';
}else{
 echo'not found';
}

the search word demo in the text may be present in the following format

1) may start with special characters/numbers Eg. "12*demo"
2) may contain special characters/numbers within the word Eg.  "de12*mo"
3) may end with special characters/numbers Eg. "demo12*"

please help I am stuck,
thanks in advance!

Note: The $input can be max. of 15 in length

  • 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-06T09:17:37+00:00Added an answer on June 6, 2026 at 9:17 am

    The Solution

    I would start by removing all special characters and numbers from the string, and then matching the word using word boundaries:

    $cleaned = preg_replace('/[^a-z ]+/i', '', 'This is dem*#o text, contains de12mo3 text, is .demo23* text');
    
    preg_match_all('/\bdemo\b/i', $cleaned, $matches, PREG_OFFSET_CAPTURE);
    
    var_dump($matches);
    

    Will give you (Codepad Demo):

    array(1) {
      [0] => array(3) {
        [0] => array(2) {
          [0] => string(4) "demo"
          [1] => int(8)
        }
    
        [1] => array(2) {
          [0] => string(4) "demo"
          [1] => int(27)
        }
    
        [2] => array(2) {
          [0] => string(4) "demo"
          [1] => int(40)
        }
      }
    }
    

    Explanation

    The first line replaces any characters in your string (called the subject argument) that match /[a-z ]+/i with ”, essentially removing the characters. The regex matches any character (or group of characters) that is not (^) the letters a-z or a space. The i flag tells regex that the search should be case insensitive (This saves us from writing a-zA-Z).

    The next line uses word boundaries to match the word ‘demo’. However, you could substitute in any word.

    New Regex Techniques

    • preg_replace()
    • preg_match_all()
    • Word Boundaries
    • Negated Character Classes
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find all the words enclosed within a {} using a regular
I want find all Saturdays and Sundays in A given month. How can I
Given a word W, I want to find all words containing the letters in
I have some words in my database. I want to find all the words
I want find all of the punctuation marks used my .txt file and give
I want to find all #tags in a piece of text (using javascript) and
I want to find all HTML tags from the input strings and removed/replace with
I want to find all excel files within a directory structure using recursion. The
I want to find all items in one collection that do not match another
I want to find all stylesheet definitions in a XHTML file with lxml.etree.findall .

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.