将 PHP preg_replace() 与字符串变量一起使用,其中变量需要表示自身的多个排列

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

使用:

preg_replace(pattern, replacement, subject)

我想 不区分大小写 替换 $My_String 中包含的文本,我可以通过以下方式实现:

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

如果 $My_Stringalpha beta gamma delta,这将识别:

等等


到目前为止,还不错。但是spaces连字符$My_String中也必须相当于。

所以preg_replace()函数需要识别这个整个集合的不区分大小写的版本:


我真的不知道该怎么做。

我能达到:

1.复制$My_String:

 $My_String_Copy = $My_String

2. 将所有 space 替换为 ¦:

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

3. 将所有连字符替换为 ¦:

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

4. 在每次出现 ¦:

时拆分 $My_String_Copy
$My_String_Copy_Array = explode('¦', $My_String_Copy);

这会给我一个数组:

$My_String_Copy_Array = [

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

];

我可以重复循环以导出上面的整个集合,作为一个数组。

之后我可以 运行 另一个循环,在这个循环中,在每次迭代中,我可以 运行:

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

依次针对上述整个集合中的每个元素。


就我所知了。但我相信一定有更聪明、更有效的方法来解决这个问题。

如果我能避免的话,我真的不想创建一个数组 包含$My_String 的每个连字符/space 排列。


示例输入和输出

字符串变量:

$My_String = 'alpha beta gamma delta';

输入:

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

输出:

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

如果您想匹配 space 或连字符上的不同字符串,您可以将 $My_String 中的所有 space 和连字符替换为 [\h-]+ 以匹配源字符串中 space 和连字符的所有连续排列。

字符 class [\h-]+- 或水平白色 space 字符匹配 1 次或多次。

您可以将模式包裹在单词边界中 \b 以防止部分匹配。

如果您回显 $My_String 的值,正则表达式将如下所示,匹配 space 或连字符。

alpha[\h-]+beta[\h-]+gamma[\h-]+delta

Regex demo | Php demo

例如

$My_String = preg_replace("/[\h-]+/", "[\h-]+", "alpha beta gamma delta");

$strings = [
    "My codephrase is alPha Beta-GaMMa deLTa and your codephrase is 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",
    "alpha-beta-gamma delta",
    "alpha-beta-gamma-delta",
    "alpha-beta-gamma-d"
];

foreach ($strings as $str) {    
    $str = preg_replace("/\b$My_String\b/i", "<strong>[=11=]</strong>", $str);
    echo $str . PHP_EOL;
}

输出

My codephrase is <strong>alPha Beta-GaMMa deLTa</strong> and your codephrase is <strong>ALpha-beTA gAmmA-DElta</strong>
<strong>alpha beta gamma delta</strong>
<strong>alpha-- -- beta    gamma delta</strong>
<strong>alpha beta     gamma-delta</strong>
<strong>alpha beta-gamma delta</strong>
<strong>alpha beta-gamma-delta</strong>
<strong>alpha-beta gamma delta</strong>
<strong>alpha-beta gamma-delta</strong>
<strong>alpha-beta-gamma delta</strong>
<strong>alpha-beta-gamma-delta</strong>
alpha-beta-gamma-d

如果您想同样对待空格和连字符,那么只需将出现的任何一个替换为 [ -] 就足够了:

$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></strong>", $My_Text) . "\n";
}

输出:

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>

感谢 @TheFourthBird@miken32 对这个问题的大力帮助。

他们的回答都很好,如果我能接受的话,我会的。

总而言之,正确的方法 - 在 运行 preg_replace() 之前 - 是获取 plaintext 字符串变量并修改它,使其不再代表 plaintext 而不是 regex

在我正在处理的脚本的最终方法中,我成功地替换了:

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

首先是:

preg_replace('/(' . preg_replace('/(.*)[\s-](.*)/', '[\s-]', $My_String) . ')/i', '<strong></strong>', $My_Text);

然后 - 在 @miken32 的更多提示之后 - 我能够使用 str_replace 作为 内部函数:

preg_replace('/(' . str_replace([" ", "-"], "[ -]", $My_String) . ')/i', '<strong></strong>', $My_Text);

如果没有上面两位成员的帮助,我永远不会想到我可以嵌套PHP字符串替换函数 以这种方式。