将数组元素转换为字符串不适用于preg_match,即使此相同字符串适用于同一preg_match


Converted array element to string doesnt work with preg_match even when this same string works with this same preg_match

我正在获取一些包含有关Wordpress自定义类型的信息的值。我需要从该文件中提取值,所以我使用 preg_match() 来完成此操作。这里是创建数组的部分(包含在问题编辑部分的文件)

        $temp_types_string = file_get_contents($this->types_file, 0);
    if ($temp_types_string == false)
    {
        return new WP_Error( 'broke', __( 'Can''t load the file with people types. Check if it wasnt deleted and get proper perrmision.', 'people_types' ) );
    }
    $temp_types_array = explode('// Custom type start' , $temp_types_string);
    $i = 0;
    $new_i = 0;
    $counter_types = count($temp_types_array);
    foreach ($temp_types_array as $type)
    {
        if (($i != 0) && ($i != ($counter_types - 1)))
        {
            $types_array[$new_i] = $type;
            $new_i++;
        }
    $i++;
    }
    //$types_array = str_replace("'", "''", $types_array);
    $this->types_array = $types_array;

其余代码如下所示,应该与名称或作为 Wordpress 自定义类型信息的任何其他值匹配。

    //$this->types_array[0] =  'if ( ! function_exists("Sample") ) { function sample_post_type() { $labels = array( "name" => "Post Types", "singular_name" => "Post Type", "menu_name" => "Post Type", "name_admin_bar" => "Post Type", "parent_item_colon" => "Parent Item:", "all_items" => "All Items", "add_new_item" => "Add New Item", "add_new" => "Add New", "new_item" => "New Item", "edit_item" => "Edit Item", "update_item" => "Update Item", "view_item" => "View Item", "search_items" => "Search Item", "not_found" => "Not found", "not_found_in_trash" => "Not found in Trash", ); $args = array( "label" => "sample_type", "description" => "Post Type Description", "labels" => $labels, "supports" => array( ), "taxonomies" => array( "category", "post_tag" ), "hierarchical" => false, "public" => true, "show_ui" => true, "show_in_menu" => true, "menu_position" => 5, "show_in_admin_bar" => true, "show_in_nav_menus" => true, "can_export" => true, "has_archive" => true, "exclude_from_search" => false, "publicly_queryable" => true, "capability_type" => "page", ); register_post_type( "sample_type", $args ); } // Hook into the "init" action add_action( "init", "sample_post_type", 0 ); } "';
$to_match = $this->types_array[0];
var_dump($to_match);
echo '<br /><br />';
//$to_match = 'if ( ! function_exists("Sample") ) { function sample_post_type() { $labels = array( "name" => "Post Types", "singular_name" => "Post Type", "menu_name" => "Post Type", "name_admin_bar" => "Post Type", "parent_item_colon" => "Parent Item:", "all_items" => "All Items", "add_new_item" => "Add New Item", "add_new" => "Add New", "new_item" => "New Item", "edit_item" => "Edit Item", "update_item" => "Update Item", "view_item" => "View Item", "search_items" => "Search Item", "not_found" => "Not found", "not_found_in_trash" => "Not found in Trash", ); $args = array( "label" => "sample_type", "description" => "Post Type Description", "labels" => $labels, "supports" => array( ), "taxonomies" => array( "category", "post_tag" ), "hierarchical" => false, "public" => true, "show_ui" => true, "show_in_menu" => true, "menu_position" => 5, "show_in_admin_bar" => true, "show_in_nav_menus" => true, "can_export" => true, "has_archive" => true, "exclude_from_search" => false, "publicly_queryable" => true, "capability_type" => "page", ); register_post_type( "sample_type", $args ); } // Hook into the "init" action add_action( "init", "sample_post_type", 0 ); } "';
    switch ($match)
    {
        case "name":
        preg_match('(("name" => ")(.*?)(",))', $to_match, $matches);
        break;
    }
var_dump($matches);

此代码的执行将显示:

string(1482) " if ( ! function_exists("Sample") ) { function sample_post_type() { $labels = array( "name" => "Post Types", "singular_name" => "Post Type", "menu_name" => "Post Type", "name_admin_bar" => "Post Type", "parent_item_colon" => "Parent Item:", "all_items" => "All Items", "add_new_item" => "Add New Item", "add_new" => "Add New", "new_item" => "New Item", "edit_item" => "Edit Item", "update_item" => "Update Item", "view_item" => "View Item", "search_items" => "Search Item", "not_found" => "Not found", "not_found_in_trash" => "Not found in Trash", ); $args = array( "label" => "sample_type", "description" => "Post Type Description", "labels" => $labels, "supports" => array( ), "taxonomies" => array( "category", "post_tag" ), "hierarchical" => false, "public" => true, "show_ui" => true, "show_in_menu" => true, "menu_position" => 5, "show_in_admin_bar" => true, "show_in_nav_menus" => true, "can_export" => true, "has_archive" => true, "exclude_from_search" => false, "publicly_queryable" => true, "capability_type" => "page", ); register_post_type( "sample_type", $args ); } // Hook into the "init" action add_action( "init", "sample_post_type", 0 ); } "
array(0) { } 

我们可以取消注释此行

   //$this->types_array[0] =  'if ( ! function_exists("Sample") ) { function sample_post_type() { $labels = array( "name" => "Post Types", "singular_name" => "Post Type", "menu_name" => "Post Type", "name_admin_bar" => "Post Type", "parent_item_colon" => "Parent Item:", "all_items" => "All Items", "add_new_item" => "Add New Item", "add_new" => "Add New", "new_item" => "New Item", "edit_item" => "Edit Item", "update_item" => "Update Item", "view_item" => "View Item", "search_items" => "Search Item", "not_found" => "Not found", "not_found_in_trash" => "Not found in Trash", ); $args = array( "label" => "sample_type", "description" => "Post Type Description", "labels" => $labels, "supports" => array( ), "taxonomies" => array( "category", "post_tag" ), "hierarchical" => false, "public" => true, "show_ui" => true, "show_in_menu" => true, "menu_position" => 5, "show_in_admin_bar" => true, "show_in_nav_menus" => true, "can_export" => true, "has_archive" => true, "exclude_from_search" => false, "publicly_queryable" => true, "capability_type" => "page", ); register_post_type( "sample_type", $args ); } // Hook into the "init" action add_action( "init", "sample_post_type", 0 ); } "';

并手动将此值分配给数组元素,我们得到

string(1157) "if ( ! function_exists("Sample") ) { function sample_post_type() { $labels = array( "name" => "Post Types", "singular_name" => "Post Type", "menu_name" => "Post Type", "name_admin_bar" => "Post Type", "parent_item_colon" => "Parent Item:", "all_items" => "All Items", "add_new_item" => "Add New Item", "add_new" => "Add New", "new_item" => "New Item", "edit_item" => "Edit Item", "update_item" => "Update Item", "view_item" => "View Item", "search_items" => "Search Item", "not_found" => "Not found", "not_found_in_trash" => "Not found in Trash", ); $args = array( "label" => "sample_type", "description" => "Post Type Description", "labels" => $labels, "supports" => array( ), "taxonomies" => array( "category", "post_tag" ), "hierarchical" => false, "public" => true, "show_ui" => true, "show_in_menu" => true, "menu_position" => 5, "show_in_admin_bar" => true, "show_in_nav_menus" => true, "can_export" => true, "has_archive" => true, "exclude_from_search" => false, "publicly_queryable" => true, "capability_type" => "page", ); register_post_type( "sample_type", $args ); } // Hook into the "init" action add_action( "init", "sample_post_type", 0 ); } ""
array(4) { [0]=> string(23) ""name" => "Post Types"," [1]=> string(11) ""name" => "" [2]=> string(10) "Post Types" [3]=> string(2) ""," } 

当然,我注意到字符串长度发生了变化,但不知道字符串长度差异的原因是什么,如果是原因,如何解决它。

@EDITIm 包括file_get_contents在第一行导入的类型文件

<?php
$empty_array = array ("empty" => "emptines");
// Custom type start
if ( ! function_exists("Sample") ) {
function sample_post_type() {
    $labels = array(
        "name"                => "Post Types",
        "singular_name"       => "Post Type",
        "menu_name"           => "Post Type",
        "name_admin_bar"      => "Post Type",
        "parent_item_colon"   => "Parent Item:",
        "all_items"           => "All Items",
        "add_new_item"        => "Add New Item",
        "add_new"             => "Add New",
        "new_item"            => "New Item",
        "edit_item"           => "Edit Item",
        "update_item"         => "Update Item",
        "view_item"           => "View Item",
        "search_items"        => "Search Item",
        "not_found"           => "Not found",
        "not_found_in_trash"  => "Not found in Trash",
    );
    $args = array(
        "label"               => "sample_type",
        "description"         => "Post Type Description",
        "labels"              => $labels,
        "supports"            => array( ),
        "taxonomies"          => array( "category", "post_tag" ),
        "hierarchical"        => false,
        "public"              => true,
        "show_ui"             => true,
        "show_in_menu"        => true,
        "menu_position"       => 5,
        "show_in_admin_bar"   => true,
        "show_in_nav_menus"   => true,
        "can_export"          => true,
        "has_archive"         => true,
        "exclude_from_search" => false,
        "publicly_queryable"  => true,
        "capability_type"     => "page",
    );
    register_post_type( "sample_type", $args );
}
// Hook into the "init" action
add_action( "init", "sample_post_type", 0 );
}
// Custom type start
?>

@EDIT2显然,字符串差异的原因是文件中的''t和',但这不是问题的解决方案,即使从数组中修剪的字符串不适用于preg_match()

在正则表达式中,您假设=>两侧都有单个空格。在您的示例文件中,它的左侧有很多空格。

尝试将您的正则表达式更改为(("name" +=> ")(.*?)(",)),它在正则表达式101中对我有用