1

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

4
  • @PhpMyCoder I am new to RegEx, this is what I am trying /[\W$demo\W]/ ,but problem is, it is not able to detect special characters present within word Eg. de123*# Commented Jun 24, 2012 at 7:23
  • regular-expressions.info Commented Jun 24, 2012 at 7:26
  • 1
    @pcdhan Good that you tried something, make sure to put it in the question next time. Commented Jun 24, 2012 at 7:30
  • @AlexLunix, my mistake, will not repeat next time. Commented Jun 24, 2012 at 7:45

3 Answers 3

3

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

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

3 Comments

@pcdhan Let us know if you need any help with the implementation.
successfully implemented, again thanks a lot, it really helped me.
@pcdhan Any time. If this answered your question you should mark it as solved by clicking the checkmark next to it.
1
/\b[^a-z]*d[^a-z]*e[^a-z]*m[^a-z]*o[^a-z]*\b/i

Matches anything but a-z between the demo characters

2 Comments

thanks @flec, the $input can be max. of 15 chars in length.
Use strlen() to check the lenght afterwards
0

Probably not efficient; But you could use this:

/(.*)d(.*)e(.*)m(.*)o(.*)/i

That would match anything.

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.