使用PHP从字符串中删除路径

时间:2018-11-21 13:19:05

标签: php arrays json preg-replace

我有这段代码,应该将数组写入json文件。这是一个简单的索引文件,将保存所有wordpress原始目录名称,我只想写字符串的最后一部分,即文件夹中包含的文件。 例如,这是我要删除的/Applications/MAMP/htdocs/wtest/admin/includes/

字符串的一部分

我尝试使用preg replace,但是没有成功,它将始终记录以下错误: PHP Warning: preg_replace(): Unknown modifier 'p'

我该如何实现?

<?php
// this is how i get the directory that will writed inside the json index file
$wp_admin_files = glob(ADMIN_PATH.'/includes/class-wp-*');
create_wp_files_index('admin_includes_index', $wp_admin_files);

// this function will write the json file
function create_wp_files_index(string $filename, array $index){
    file_put_contents(TMP_DIR.'/'.$filename, json_encode($index, JSON_UNESCAPED_SLASHES));
    return true;
}

?>

JSON文件内容示例

["/Applications/MAMP/htdocs/wtest/admin/includes/class-wp-ajax-upgrader-skin.php",
"/Applications/MAMP/htdocs/wtest/admin/includes/class-wp-automatic-updater.php",
"/Applications/MAMP/htdocs/wtest/admin/includes/class-wp-comments-list-table.php",
"/Applications/MAMP/htdocs/wtest/admin/includes/class-wp-community-events.php"]

0 个答案:

没有答案