PHP:数组中的字符串换行
使用下面的方法,我可以匹配数组中的所有项,但我只能替换预期的匹配值。是否有一种方法可以执行相同的操作,但不是直接替换,而是使用指定的值包装匹配的文本 当前代码: 电流输出 通缉犯PHP:数组中的字符串换行,php,arrays,string,Php,Arrays,String,使用下面的方法,我可以匹配数组中的所有项,但我只能替换预期的匹配值。是否有一种方法可以执行相同的操作,但不是直接替换,而是使用指定的值包装匹配的文本 当前代码: 电流输出 通缉犯 我想你可以用array_map。以下是我的例子: $COLORS=阵列(“红色”、“蓝色”、“绿色”) $string=“有三堵红色和绿色的墙。” $func=函数($value)使用($colors){ }) $text=array_map($func,explode(“,$string)) $text=内爆(“,$
我想你可以用array_map。以下是我的例子: $COLORS=阵列(“红色”、“蓝色”、“绿色”) $string=“有三堵红色和绿色的墙。” $func=函数($value)使用($colors){ }) $text=array_map($func,explode(“,$string)) $text=内爆(“,$text”) echo$文本;
`您需要的是
preg\u replace
。您将能够用包含匹配项的字符串替换匹配项。在这种情况下,%1%$0%2%“
其中,$0
将替换为匹配项
但是,您需要引用(使用preg_quote
)并向数组的成员添加分隔符。我们可以使用array\u map
来实现这一点
代码如下:
echo preg_replace
(
array_map
(
function($input)
{
return "/".preg_quote($input, "/")."/";
},
$colours
),
"%1%$0%2%",
$string
);
不,我不建议假设它们在没有引用的情况下是安全的,即使在这种情况下它们是安全的。是的,用它们创建一个单一的正则表达式模式是可行的,但是为什么要麻烦呢?最简单但不是最有效的:
$colours = array("Red", "Blue", "Green");
$colours_replacment = array('%1%Red%2%', '%1%Blue%2%', '%1%Green%2%');
$string = "There are three Red and Green walls.";
echo str_replace($colours, $colours_replacment, $string);
$colours = array("Red", "Blue", "Green");
$string = "There are three Red and Green walls.";
foreach($colours as $colour) {
$string = str_replace($colour, '%1%' . $colour . '%2%', $string);
}
echo $string;
简单有效:
$colours = array("Red", "Blue", "Green");
$colours_replacment = array('%1%Red%2%', '%1%Blue%2%', '%1%Green%2%');
$string = "There are three Red and Green walls.";
echo str_replace($colours, $colours_replacment, $string);
$colours = array("Red", "Blue", "Green");
$string = "There are three Red and Green walls.";
foreach($colours as $colour) {
$string = str_replace($colour, '%1%' . $colour . '%2%', $string);
}
echo $string;
根据您是否要考虑上下文,解决方案会有所不同 如果您计划只在任何上下文中匹配字符串,并使用
%1%
和%2%
随附的副本重新组合字符串,则需要使用
preg\u replace(“/”。内爆(“;”,数组映射(函数($i){return preg\u quote($i,“/”;},$colors))。“/”,“%1%$0%2%”,$string)
这里使用
或运算符形成正则表达式,$colors
数组中的项目中的所有特殊字符都使用\
函数通过\
进行转义。替换中的$0
指的是整个匹配值
如果$colors
是带有空格的短语,则需要对项目进行排序,以便将较长的字符串放在第一位:
rsort($colors,SORT_FLAG_CASE | SORT_STRING);
回显“\n”。preg_replace(“/”。内爆(“;”,数组映射(函数($i){return preg_quote($i,“/”;},$colors))。“/”,“%1%$0%2%”,$string);
如果这些$colors
是简单的英文字母单词,并且您希望将它们作为整个单词进行匹配,则不需要preg_quote
,您可以使用单词边界,如:
preg_replace(“/\b(?“.intlode(“|”),$colors)。”\b/“,“%1%$0%2%”,$string)
要使搜索不区分大小写,请在preg\u replace
第一个参数中的last/
regex分隔符后添加i
标志
见:
$colors=数组(“红色”、“蓝色”、“绿色”);
$string=“有三堵红色和绿色的墙。”;
echo preg_replace(“/”。内爆(“;”),数组映射(函数($i){return preg_quote($i,“/”;},$colors))。“/”,“%1%$0%2%”,$string);
//=>有三个%1%红色%2%和%1%绿色%2%的墙。
回显“\n”。preg_replace(“/\b(?“.内爆(“|”),$colors)。”\b/“,“%1%$0%2%”,$string);
//=>有三个%1%红色%2%和%1%绿色%2%的墙。
$colors=数组(“红色”、“蓝色”、“绿色”、“蓝色牛仔裤”);
$string=“有三面红绿相间的墙和一条蓝色牛仔裤。”;
rsort($colors,SORT_FLAG_CASE | SORT_STRING);
回显“\n”。preg_replace(“/”。内爆(“;”,数组映射(函数($i){return preg_quote($i,“/”;},$colors))。“/”,“%1%$0%2%”,$string);
//=>有三个%1%红色%2%和%1%绿色%2%墙壁和%1%蓝色牛仔裤%2%。
如果您想要一个原始解决方案,那么也为替换提供一个数组,每个数组中都包含%1%…%2%
的单词。您的意思是preg\u replace(“/\b(?”.inpode(“|”),$colors)。”\b/“,“%1%$0%2%,$string)
?有没有可能在不影响已包装字符串的情况下使其成为不区分大小写的检查?我已经使用了它,而且它似乎做到了,它有意义吗?“/\b(?I:“.intomode(“|”),$colors)。”\b/“
@llanato如果您只需要以不区分大小写的方式查找术语,并包装找到的匹配项,只需添加I
标志,”/\b(?:.内爆(“|”,$colors)。”\b/i“
$colours = array("Red", "Blue", "Green");
$colours_replacment = array('%1%Red%2%', '%1%Blue%2%', '%1%Green%2%');
$string = "There are three Red and Green walls.";
echo str_replace($colours, $colours_replacment, $string);
$colours = array("Red", "Blue", "Green");
$string = "There are three Red and Green walls.";
foreach($colours as $colour) {
$string = str_replace($colour, '%1%' . $colour . '%2%', $string);
}
echo $string;