PHP初学者,看起来很有趣。我已经设法把逗号和"=>"放在我需要的地方,但我也需要在每个单词周围加引号。我不知道该如何处理那部分。
我试图用字符串中的键和值构建一个漂亮的数组。
这是字符串的开头
$dummy = "ac whois.nic.ac
ae whois.aeda.net.ae
aero whois.aero
af whois.nic.af
ag whois.nic.ag
...."
我需要的是
"ac" => "whois.nic.ac",
"ae" => "whois.aeda.net.ae",
"aero" => "whois.aero",
"af" => "whois.nic.af",
"ag" => "whois.nic.ag",
到目前为止我所得到的。。。
ac => whois.nic.ac,
ae => whois.aeda.net.ae,
aero => whois.aero,
af => whois.nic.af,
ag => whois.nic.ag,
在每个"/n"上创建一个逗号
$strg = preg_replace("/(?<![.])(?=['n'r]|$)/", ",", $dummy);
在每个空间上创建"=>"
$strg = str_replace(' ', ' => ', $strg);
干杯。。
我会在不使用preg_replace
:的情况下执行此操作
$dummy = "ac whois.nic.ac
ae whois.aeda.net.ae
aero whois.aero
af whois.nic.af
ag whois.nic.ag
....";
$lines = preg_split('['r'n]+', $dummy, null, PREG_SPLIT_NO_EMPTY);
$lines = array_filter(array_map(function($line){
$line = trim($line);
$words = explode(" ", $line, 2);
if(count($words) !== 2) return null;
return "'"$words[0]'" => '"$words[1]'",";
}));
$output = implode("'n", $lines); // or whatever EOL it was