Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.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_String - Fatal编程技术网

Php 如何减少这个字符串操作代码

Php 如何减少这个字符串操作代码,php,string,Php,String,我写了下面的代码,有点复杂。是否有任何修改可以在较短的步骤内减少以下代码,而不是将其级联 $countries='IN,US,AU,MY,TH,KR,TW'; //list country codes separated by comma $countries=explode(',',$countries); //partition from , $countries=implode('\',\'',$countries); //add ' at start and e

我写了下面的代码,有点复杂。是否有任何修改可以在较短的步骤内减少以下代码,而不是将其级联

$countries='IN,US,AU,MY,TH,KR,TW';      //list country codes separated by comma
$countries=explode(',',$countries);     //partition from ,
$countries=implode('\',\'',$countries);     //add ' at start and end of the country code
$countries="'".$countries."'";              //add ' to first and last country code
$countries=explode(',',$countries);         //create array by breaking it from ,
print_r($countries);
输出

数组([0]=>'IN'[1]=>'US'[2]=>'AU'[3]=>'MY'[4]=>'TH'[5] =>“KR”[6]=>“TW”)

对于如下输出:

$countries='IN,US,AU,MY,TH,KR,TW';      //list country codes separated by comma
$countries= explode(',',$countries);     //partition from ,
$countries= "'".implode("','",$countries)."'";    

$countries=explode(',',$countries);         //create array by breaking it from ,
print_r($countries);
数组([0]=>'IN'[1]=>'US'[2]=>'AU'[3]=>'MY'[4]=>'TH'[5]=>'KR'[6]=>'TW')

您可以执行以下操作:

$countries='IN,US,AU,MY,TH,KR,TW';      //list country codes separated by comma
$countries= explode(',',$countries);     //partition from ,
$countries= "'".implode("','",$countries)."'";    

$countries=explode(',',$countries);         //create array by breaking it from ,
print_r($countries);
你可以这样做

$countries='IN,US,AU,MY,TH,KR,TW';     
$countries=explode(',',$countries);    
$countries2 = array_map(function($val) { return "'".$val."'";} , $countries);
print_r($countries2);
输出

数组([0]=>'IN'[1]=>'US'[2]=>'AU'[3]=>'MY'[4]=>'TH'[5]=>'KR'[6]=>'TW')


您可以使用如下正则表达式:

$countries = 'IN,US,AU,MY,TH,KR,TW';
$countries = preg_replace('/([A-z]{2})/', "'$1'", $countries);
$countries = explode(',',$countries);

$1,$2$正则表达式中的n
替换是对括号中的匹配项的引用
$0
将是整个匹配,
$1
将是第一个括号中的捕获,
$2
将是第二个,等等

$1 is a reference to whatever is matched by the first (.*)
$2 is a reference to (\?|&)
$4 is a reference to the second (.*)

输出

数组([0]=>'IN'[1]=>'US'[2]=>'AU'[3]=>'MY'[4]=>'TH' [5] =>“KR”[6]=>“TW”)


ref:

所以您想要一个最终数组,它来自您的初始字符串,但值周围有
”?也许preg_replace可以在您的循环中执行,在循环中打印值添加“$value”,比如-
$countries=“””。str_replace(“,”,“,”,“,$countries)。“”$国家=爆炸(“,”,$国家)“$1”的作用是什么?$1表示正则表达式中()之间的匹配,如果您有一个带有多个()的正则表达式,则可以使用$1作为()之间的第一个匹配,$2秒的匹配,数字可以从$0到$99。您可以在“$1”做什么?中找到preg_replace,它告诉preg_replace在搜索匹配之前添加一个。这场比赛在preg中被称为$1_replace@kpatil哇!两天后,我的答案还是不正确,甚至是不感谢?你们接受了罗汉人的答案,这或多或少是我的一份副本,贴在我的答案后面
$countries = 'IN,US,AU,MY,TH,KR,TW';
$countries = explode(',',preg_replace('/([A-Z]{2})/', "'$1'", $countries));
print_r($countries);