Skip to content
Advertisement

Using PHP preg_replace() with a string variable, where the variable needs to represent multiple permutations of itself

Using:

preg_replace(pattern, replacement, subject)

I’d like to replace, case-insensitively, the text contained in $My_String, which I can achieve with:

preg_replace('/('.$My_String.')/i', '<strong>$1</strong>', $My_Text);

If $My_String is alpha beta gamma delta, this will identify:

  • alPhA beTa GammA DeLta
  • aLpHa bEta gAMma delTa

etc.


So far, so good. But any spaces and hyphens in $My_String must also be regarded as equivalent.

So the preg_replace() function also needs to identify case-insensitive versions of this entire set:

  • alpha beta gamma delta
  • alpha beta gamma-delta
  • alpha beta-gamma delta
  • alpha beta-gamma-delta
  • alpha-beta gamma delta
  • alpha-beta gamma-delta
  • alpha-beta-gamma delta
  • alpha-beta-gamma-delta

I’m genuinely not sure how to go about this.

I can get as far as:

1. Make a copy of $My_String:

 $My_String_Copy = $My_String

2. Replace all the spaces with ¦:

 $My_String_Copy = str_replace(' ', '¦', $My_String_Copy);

3. Replace all hyphens with ¦:

$My_String_Copy = str_replace('-', '¦', $My_String_Copy);

4. Split $My_String_Copy at every occurrence of ¦:

$My_String_Copy_Array = explode('¦', $My_String_Copy);

This will give me an array:

$My_String_Copy_Array = [

  'Alpha',
  'Beta',
  'Gamma',
  'Delta'

];

Which I can repeatedly loop through to derive the entire set above, as an array.

After that I can run yet another loop, in which, in each iteration, I can run:

preg_replace('/('.$Entire_Set[$i].')/i', '<strong>$1</strong>', $My_Text);

targeting, in turn, each element of the entire set above.


That’s as far as I’ve got. But I’m sure there must be a smarter, more efficient way to go about this.

If I can possibly avoid it, I really don’t want to create an array containing every single hyphen / space permutation of $My_String.


Example Input and Output

String Variable:

$My_String = 'alpha beta gamma delta';

Input:

My codephrase is alPha Beta-GaMMa deLTa and your codephrase is ALpha-beTA gAmmA-DElta.

Output:

My codephrase is <strong>alPha Beta-GaMMa deLTa</strong> and your codephrase is <strong>ALpha-beTA gAmmA-DElta</strong>.

Advertisement

Answer

If you want to treat spaces and hyphens the same, then simply replacing any occurrence of either with [ -] should be sufficient:

$samples = [
    "My codephrase is alPhA beTa GammA DeLta and your codephrase is aLpHa bEta gAMma delTa",
    "My codephrase is alpha beta gamma delta and your codephrase is alpha beta gamma-delta",
    "My codephrase is alpha beta-gamma delta and your codephrase is alpha beta-gamma-delta",
    "My codephrase is alpha-beta gamma delta and your codephrase is alpha-beta gamma-delta",
    "My codephrase is alpha-beta-gamma delta and your codephrase is alpha-beta-gamma-delta",
];

$My_String = "alpha beta gamma delta";

$My_String = str_replace([" ", "-"], "[ -]", $My_String);
foreach ($samples as $My_Text) {
    echo preg_replace("/b($My_String)b/i", "<strong>$1</strong>", $My_Text) . "n";
}

Output:

My codephrase is <strong>alPhA beTa GammA DeLta</strong> and your codephrase is <strong>aLpHa bEta gAMma delTa</strong>
My codephrase is <strong>alpha beta gamma delta</strong> and your codephrase is <strong>alpha beta gamma-delta</strong>
My codephrase is <strong>alpha beta-gamma delta</strong> and your codephrase is <strong>alpha beta-gamma-delta</strong>
My codephrase is <strong>alpha-beta gamma delta</strong> and your codephrase is <strong>alpha-beta gamma-delta</strong>
My codephrase is <strong>alpha-beta-gamma delta</strong> and your codephrase is <strong>alpha-beta-gamma-delta</strong>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement