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

Php 字符串替换不起作用。我是不是忽略了什么?

Php 字符串替换不起作用。我是不是忽略了什么?,php,wordpress,Php,Wordpress,我正在尽力为Tom Mc Farlin的WordPress插件样板创建一个生成器。一切都很顺利。我从github加载文件,将其解压缩到一个目录,并替换所有需要的字符串,如“plugin\u name”、“Your name”等 不幸的是,有一个名为$plugin_name的受保护类变量和一些其他小的位。因此,我决定在更换后“修复”一些缺陷,如下所示: // Repair some flaws $repair_file = $newAbsDir.'/plugin-name/trunk/includ

我正在尽力为Tom Mc Farlin的WordPress插件样板创建一个生成器。一切都很顺利。我从github加载文件,将其解压缩到一个目录,并替换所有需要的字符串,如“plugin\u name”、“Your name”等

不幸的是,有一个名为$plugin_name的受保护类变量和一些其他小的位。因此,我决定在更换后“修复”一些缺陷,如下所示:

// Repair some flaws
$repair_file = $newAbsDir.'/plugin-name/trunk/includes/class-'.$new_plugin_name.'.php';
$repair_file_content = file_get_contents($repair_file);
$repair_strings = array(
    '$'.$new_plugin_name => '$plugin_name', 
    '$this->'.$new_plugin_name => '$this->plugin_name', 
    'get_'.$new_plugin_name => 'get_plugin_name'
);
foreach($repair_strings as $string => $replace){
    $repair_file_content = str_replace($string, $replace, $repair_file_content);
}
file_put_contents($repair_file, $repair_file_content);
但是,似乎对我的glob文件数组非常有效的东西,根本不适用于上述内容。我想这和美元符号有关。有人知道如何解决这个问题吗?

来自:

如果searchreplace是数组,则
str_replace()
从每个数组中获取一个值,并使用它们在主题上搜索和替换

因此,这就像在不使用foreach()的情况下执行一个简单的
str_replace()
。数组值在文本数组上传递,键在上传递。由于要搜索的是数组键,因此必须反转数组:

$repair_strings = array(
    '$plugin_name' => '$'.$new_plugin_name, 
    '$this->plugin_name' => '$this->'.$new_plugin_name, 
    'get_plugin_name' => 'get_'.$new_plugin_name
);
$repair_file_content = str_replace( array_keys( $repair_strings ), $repair_strings, $repair_file_content );
反转数组只是为了保持逻辑
search=>replace
,但要使用原始数组,则需要:

$repair_file_content = str_replace( $repair_strings, array_keys( $repair_strings ), $repair_file_content );

D'oh…


$My_fantastic_插件与$My_fantastic_插件略有不同。抱歉给您带来麻烦,感谢您对
$repair\u file\u content=str\u replace(数组密钥($repair\u strings),$repair\u strings,$repair\u file\u content)的良好建议

完全不清楚您试图替换的是什么,确切地说是什么不起作用?你是否成功地浏览了所有文件?有些事情正在改变吗?错误的事情?是否正确写入完成的文件?您是否检查了
$repair\u strings
数组是否正确填充?您是否尝试过用固定数组替换它以查看其是否有效?为什么使用
foreach
str_replace
可以处理数组:
str_replace(数组键($repairStrings),$repairStrings,$content)
省去了在数组中循环的麻烦,也省去了一遍又一遍地调用整个字符串的
str\u replace
,因为我没有很好地解释这个问题。我尝试了一些简化的方法,但也不起作用。让我们假设如下:我创建了一个名为“My fantastic plugin”的新插件。在克隆过程中,我将所有出现的字符串“Plugin\u Name”替换为字符串“My\u-Plugin”。不幸的是,它替换了三个不应该被替换的部分。下面是我试图实现的目标:我给变量$new\u plugin\u name='My\u fantastic\u plugin'。在那之后,我寻找字符串“$My_fantastic_plugin”,并想用字符串“$plugin_name”替换它。我这样尝试:
//修复一些缺陷$Repair_file=$newAbsDir./plugin name/trunk/includes/class-”.$new_plugin_name..php”$修复文件内容=文件获取内容($repair\u file)$字符串\'u to \'u search \'u for='$。$new \'u plugin \'u name$字符串_to_替换为=“$plugin_name”$repair\u file\u content=str\u replace($string\u to\u search\u for,$string\u to\u replace\u with,$repair\u file\u content);文件内容($repair\u file,$repair\u file\u content)但更换根本没有发生:(