普通网友 2015-06-20 19:59
浏览 54
已采纳

转换后的数组元素到字符串不能与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 2015-06-21 14:43
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?