Allan Karlson
Allan Karlson

Reputation: 499

php request of a specific word but separable by spaces

I have a question concerning regex, i.e. with the syntax that works with php. I know some basics about regex in php, so I know that I can match something with

preg_match("/[maxmustermann ]/u", $input_line, $output_array);

Now I want to match all text which contains optional several words which by could be seperated by an space.

Sorry I just dont know how to ask. I try to make an example. I have this text and want to match all the bold ones.

orem ipsum dolor sit amet, consectetur max adipiscing elit. Proin maxm pellentesque dui maxmustermann eu erat mustermann rhoncus tempor sit amet quis odio. Max Mustermann habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Max Muster Mann et malesuada fames ac ante ipsum primis in faucibus. Nam vitae nisl dui.

That means I have this two words: max and mustermann and I want to match words (one ore more) which containing the letters of max and/or mustermann in the order how they are but also random placed space.

Thank you

Upvotes: 1

Views: 67

Answers (3)

user557597
user557597

Reputation:

Edit update:

After re-reading the question, this is a revised answer.
Just do a preg_match_all using the regex below.

OP criteria:

I want to match all text which contains optional several words which by could be seperated by an space. ...

I want to match words (one ore more) which containing the letters of max and/or mustermann in the order how they are but also random placed space.

For this you need to use whitespace boundary's.
All the item substrings are kept ordered by a alternative word boundary.

This regex will also match a group of substrings separated by whitespace.

(?i)(?<!\S)(?!\s)(?:m|\b)(?:a|\b)(?:x|\b)(?:m|\b)(?:u|\b)(?:s|\b)(?:t|\b)(?:e|\b)(?:r|\b)(?:m|\b)(?:a|\b)(?:n|\b)(?:n|\b)(?:\s+(?!\s)(?:m|\b)(?:a|\b)(?:x|\b)(?:m|\b)(?:u|\b)(?:s|\b)(?:t|\b)(?:e|\b)(?:r|\b)(?:m|\b)(?:a|\b)(?:n|\b)(?:n|\b))*(?!\S)

101 demo

Benchmark

Regex1:   (?i)(?<!\S)(?!\s)(?:m|\b)(?:a|\b)(?:x|\b)(?:m|\b)(?:u|\b)(?:s|\b)(?:t|\b)(?:e|\b)(?:r|\b)(?:m|\b)(?:a|\b)(?:n|\b)(?:n|\b)(?:\s+(?!\s)(?:m|\b)(?:a|\b)(?:x|\b)(?:m|\b)(?:u|\b)(?:s|\b)(?:t|\b)(?:e|\b)(?:r|\b)(?:m|\b)(?:a|\b)(?:n|\b)(?:n|\b))*(?!\S)
Completed iterations:   50  /  50     ( x 1000 )
Matches found per iteration:   6
Elapsed Time:    10.42 s,   10421.84 ms,   10421843 µs

Explained

 (?i)                  # Case insensitive modifier
 (?<! \S )             # Whitespace boundary behind
 (?! \s )              # Insure one of the next substrings match

 (?: m | \b )
 (?: a | \b )
 (?: x | \b )
 (?: m | \b )
 (?: u | \b )
 (?: s | \b )
 (?: t | \b )
 (?: e | \b )
 (?: r | \b )
 (?: m | \b )
 (?: a | \b )
 (?: n | \b )
 (?: n | \b )

 (?:
      \s+                   # Optional space and more words
      (?! \s )              # Insure one of the next substrings match
      (?: m | \b )
      (?: a | \b )
      (?: x | \b )
      (?: m | \b )
      (?: u | \b )
      (?: s | \b )
      (?: t | \b )
      (?: e | \b )
      (?: r | \b )
      (?: m | \b )
      (?: a | \b )
      (?: n | \b )
      (?: n | \b )
 )*
 (?! \S )              # Whitespace boundary ahead

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89565

You can't do that with regex only. You need first to extract all words composed with your selected letters, and in a second time you have to filter these words. Something like this:

$word = 'maxmustermann';
preg_match_all('~\b[aemnrstux]+\b~ui', $txt, $matches);

$result = array_filter($matches[0], function ($i) use ($word) {
    return stripos($word, $i) !== false;
});

demo

You can proceed a similar way if you want to perform a replacement:

$word = 'maxmustermann';
$result = preg_replace_callback('~\b[aemnrstux]+\b~ui', function ($m) use ($word) {
    return stripos($word, $m[0]) !== false ? "#{$m[0]}#" : $m[0];
}, $txt);

demo

Upvotes: 2

Jay Blanchard
Jay Blanchard

Reputation: 34416

You want a case-insensitive match using this regex:

/(max)|(muster)|(mann)/i

EXAMPLE

EDIT: Thanks to @AbraCadaver for pointing out you'll also need preg_match_all()

Upvotes: 0

Related Questions