dongu189744321158
dongu189744321158
2015-06-20 19:59

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

已采纳

Im getting some values that contains information about Wordpress Custom types. I need to extract the values from that file so I used preg_match() to accomplish that. Here the part that create the array (included file in edited part of question)

        $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;

Rest of code looks like this and that should match name or any other value that is Wordpress custom type information.

    //$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);

Execution of this code will show:

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) { } 

We can just uncomment this line

   //$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 ); } "';

and assign this value manually to array element and we gets

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) ""," } 

Of course I noticed that string lenght has changed, but have no idea what is the reason of string lenght difference and if its reason, how to fix it.

@EDIT Im including types file that file_get_contents import in first line

<?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 Obviously the reason of string difference was \t and in file, but this is not the solution to the problem, even when trimmed string from array doesnt work with preg_match()

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duannaoye0732 duannaoye0732 6年前

    In you regex you assume single spaces on both sides of =>. In you example file you have many spaces on the left side of it.

    Try changing you regex to (("name" +=> ")(.*?)(",)), it works for me in regex101

    点赞 评论 复制链接分享