转换后的数组元素到字符串不能与preg_match一起使用,即使这个相同的字符串与同一个preg_match一起使用也是如此

时间:2015-06-20 19:59:06

标签: php arrays regex string 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) ""," } 

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

@EDIT 我包括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和\ n,但这不是解决问题的方法,即使数组中的修剪字符串不能与preg_match()一起使用

1 个答案:

答案 0 :(得分:0)

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

尝试将正则表达式更改为(("name" +=> ")(.*?)(",)),它适用于我regex101