我正在构建自己的插件系统,用于添加新的"项目媒体类型"到我的WordPress项目组合插件。
我正在使用WP_List_Table
WP类创建项目媒体类型插件的列表,并允许通过安装,激活,禁用和删除每个插件来管理它们。
我的代码使用PHP扫描目录以检测有效的项目媒体类型插件
它在此预设文件夹中找到的每个文件然后读取文件的标题注释数据,就像真正的WordPress插件一样,以获取有关该插件的信息。
下面是我的函数,它扫描目录并在检测到匹配时将每个匹配文件中的标题注释数据读入PHP变量...
public function get_project_media_types_in_file_system ($from_folder = PLUGINS_FOLDER)
{
if ($handle = opendir($from_folder)) {
while ($file = readdir($handle)) {
echo $file.'rrrrrrr<hr>';
if (is_file($from_folder . $file)) {
if (strpos($from_folder . $file, '.php')) {
$fp = fopen($from_folder . $file, 'r');
// Pull only the first 8kiB of the file in.
$plugin_data = fread($fp, 8192);
fclose($fp);
preg_match('|Project Media Type Name:(.*)$|mi', $plugin_data, $project_media_type_name);
preg_match('|Version:(.*)|i', $plugin_data, $version);
preg_match('|Description:(.*)$|mi', $plugin_data, $description);
preg_match('|Admin Selector Image:(.*)$|mi', $plugin_data, $admin_selector_image);
preg_match('|Screenshot Image 1:(.*)$|mi', $plugin_data, $screenshot_image_1);
preg_match('|Screenshot Image 2:(.*)$|mi', $plugin_data, $screenshot_image_2);
preg_match('|Author:(.*)$|mi', $plugin_data, $author_name);
preg_match('|Author Email:(.*)$|mi', $plugin_data, $author_email);
preg_match('|Author URI:(.*)$|mi', $plugin_data, $author_uri);
foreach (array(
'project_media_type_name' ,
'version' ,
'description',
'admin_selector_image',
'screenshot_image_1',
'screenshot_image_2',
'author_name',
'author_email',
'author_uri'
) as $field) {
if (! empty(${$field})){
${$field} = trim(${$field}[1]);
}else{
${$field} = '';
}
}
$project_media_type_plugin_data = array(
'file_name' => $file,
'project_media_type_name' => $project_media_type_name,
'class_name' => $project_media_type_name,
'title' => $project_media_type_name,
'description' => $description,
'version' => $version,
'admin_selector_image' => $admin_selector_image,
'screenshot_image_1' => $screenshot_image_1,
'screenshot_image_2' => $screenshot_image_2,
'author_name' => $author_name,
'author_email' => $author_email,
'author_uri' => $author_uri,
'installed' => 'no',
'active' => 0
);
$this->project_media_type_plugin_headers[] = $project_media_type_plugin_data;
}
} else{
if ((is_dir($from_folder . $file)) && (substr($file,0,1) != '.')) {
$this->get_project_media_types_in_file_system($from_folder . $file . '/');
}
}
}
closedir($handle);
}
return $this->project_media_type_plugin_headers;
}
此代码的行:
preg_match('|Screenshot Image 1:(.*)$|mi', $plugin_data, $screenshot_image_1);
之后是另一条类似的行,数字1更改为数字2。
我现在的目标是修改此代码,使其找到Screenshot Image
而不是Screenshot Image 1
或Screenshot Image 2
我希望能够拥有Screenshot Image
的任意数量的行,并将每个匹配的行添加到数组中,以便插件可以拥有他们喜欢的任意数量的图像。
目前这是插件标题的格式:
<?php
/**
* Project Media Type Name: coverflow slider Test Plugin
* Version: 2.0.0
* Description: coverflow slider Test Plugin description
* Admin Selector Image: https://www.apollowebstudio.com/screenshots/2016/ApplicationFrameHost_2016-07-28_14-39-46.png
* Screenshot Image 1: https://www.apollowebstudio.com/screenshots/2016/Books-Animation.gif
* Screenshot Image 2: https://www.apollowebstudio.com/screenshots/2016/2013-05-20_23-11-40.png
* Author: Jason Davis
* Author Email: jason.davis.fl@gmail.com
* Author URI: https://www.apollowebstudio.com
*/
使用新版本,它可以在下面看起来像这样,每个图像将被添加到数组而不是带有单个字符串的变量:
<?php
/**
* Project Media Type Name: coverflow slider Test Plugin
* Version: 2.0.0
* Description: coverflow slider Test Plugin description
* Admin Selector Image: https://www.apollowebstudio.com/screenshots/2016/ApplicationFrameHost_2016-07-28_14-39-46.png
* Screenshot Image: https://www.apollowebstudio.com/screenshots/2016/Books-Animation.gif
* Screenshot Image: https://www.apollowebstudio.com/screenshots/2016/2013-05-20_23-11-40.png
* Screenshot Image: https://www.apollowebstudio.com/screenshots/2016/Books-Animation3.gif
* Screenshot Image: https://www.apollowebstudio.com/screenshots/2016/Books-Animation4.gif
* Author: Jason Davis
* Author Email: jason.davis.fl@gmail.com
* Author URI: https://www.apollowebstudio.com
*/
任何帮助表示赞赏
答案 0 :(得分:1)
您可以使用这样的简单正则表达式来解决这个问题:
preg_match('|Screenshot Image \d+:(.*)$|mi', $plugin_data, $screenshot_image_1);
请记住,如果您多次使用它,$ screenshot_image_1将被覆盖。
也许您应该将其更改为 preg_match_all ,而不是这种情况会将所有匹配项存储在数组中。
preg_match_all('|Screenshot Image \d+:(.*)$|mi', $plugin_data, $screenshot_image_matches);
<强> Regex demo 强>
<强> IDE One demo 强>