当前位置:Gxlcms > PHP教程 > 帮我看下这ARRAY哪里错了

帮我看下这ARRAY哪里错了

时间:2021-07-01 10:21:17 帮助过:5人阅读

function replace_text_wps($text){
$replace = array(
// '关键词' => '替换的关键词'
'wordpress' => 'wordpress');
$tmpfilename='1.txt';//格式为prom|http://www.baidu.com
$str = file_get_contents($tmpfilename);//获得内容
$arr = explode("\n",$str);//分行存入数组
foreach($arr as $row){//遍历每行
$keys=explode("|",$row);
$replace[$keys[0]]=$keys[1]; //这里为什么没有成功添加进去呢
}


$replace['excerpt'] = 'cool';
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
echo replace_text_wps('prom dresses wordpress excerpt');
?>


回复讨论(解决方案)

print_r($replace) ; 看看

你也可以用 $arr=file($tmpfilename);
代替
$str = file_get_contents($tmpfilename);//获得内容
$arr = explode("\n",$str);//分行存入数组

两者效果一样,且file更简洁一些。

foreach($arr as &$row){ //添加引用
$keys=explode("|",$row);
$replace[$keys[0]]=$keys[1];
}

人气教程排行