Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php匹配整个字符串_Php_Preg Match - Fatal编程技术网

如何使用php匹配整个字符串

如何使用php匹配整个字符串,php,preg-match,Php,Preg Match,我有两个阵列: $search = array('A/OPTION-1/z','/OPTION-2/','/OPTION-3/','/OPTION-4/','/OPTION-5/','/OPTION-6/','/OPTION-7/','/OPTION-8/', '/OPTION-9/','/OPTION-10/','/OPTION-11/','/OPTION-12/','/OPTION-13/','/OPTION-14/','/OPTION-15/','/OPTION-

我有两个阵列:

 $search = array('A/OPTION-1/z','/OPTION-2/','/OPTION-3/','/OPTION-4/','/OPTION-5/','/OPTION-6/','/OPTION-7/','/OPTION-8/',
            '/OPTION-9/','/OPTION-10/','/OPTION-11/','/OPTION-12/','/OPTION-13/','/OPTION-14/','/OPTION-15/','/OPTION-16/','/OPTION-17/',
            '/OPTION-18/','/OPTION-19/');
并希望替换为:

$replace = array('<b class="option" id="1" >OPTION 1</b><hr>',
            '<b class="option" id="2">OPTION 2</b><hr>',
            '<b class="option" id="3">OPTION 3</b><hr>',
            '<b class="option" id="4">OPTION 4</b><hr>',
            '<b class="option" id="5">OPTION 5</b><hr>',
            '<b class="option" id="6">OPTION 6</b><hr>',
            '<b class="option" id="7">OPTION 7</b><hr>',
            '<b class="option" id="8">OPTION 8</b><hr>',
            '<b class="option" id="9">OPTION 9</b><hr>',
            '<b class="option" id="10">OPTION 10</b><hr>',
            '<b class="option" id="11">OPTION 11</b><hr>',
            '<b class="option" id="12">OPTION 12</b><hr>',
            '<b class="option" id="13">OPTION 13</b><hr>',
            '<b class="option" id="14">OPTION 14</b><hr>',
            '<b class="option" id="15">OPTION 15</b><hr>',
            '<b class="option" id="16">OPTION 16</b><hr>',
            '<b class="option" id="17">OPTION 17</b><hr>',
            '<b class="option" id="18">OPTION 18</b><hr>',
            '<b class="option" id="19">OPTION 19</b><hr>');

问题是,当我替换选项1时,它工作正常,但当我替换选项11时,它将选项11替换为选项1,应该是选项11。

尝试更改“替换”的顺序。首先对具有较大数字的选项运行“替换”。

您可以使用此常规规则捕获任何
选项X
字符串并替换它们:

preg_replace("/OPTION (\d+)/i", "<b class="option" id="$1">OPTION $1</b><hr>", $subject_string);
preg\u replace(“/OPTION(\d+)/i”,“OPTION$1
”,$subject\u字符串);
我还打算建议更改数组的顺序

你可以用它来做这件事

使用单词边界(即
\b
):


$
匹配PCREI中的字符串结尾。我不明白,您是否试图将
选项X
替换为
选项X
?我正在尝试搜索选项1,并希望使用preg_replace($search,$replace,$pqreplace,-1)替换选项1+1对于好的解决方案:)谢谢大家。。。非常感谢:)
preg_replace("/OPTION (\d+)/i", "<b class="option" id="$1">OPTION $1</b><hr>", $subject_string);
$search = array('/\bOPTION-1\b/', '/\bOPTION-2\b/', ... );