dq1230123 2017-11-07 03:51
浏览 52
已采纳

未定义的偏移:2使用php中的preg_split

I am currently developing a system. I have tab menu in user, in here all the menus will be display. I have module called add menu where the admin will add another menu, after creating a new menu it will be added to the tab menu of the user. I have given menus like Sizzling Plate and Tacos, the things that I've done is when the user click the Sizzling Plate it will redirect you to that menu

Like this: localhost:/xxxx/SizzlingPlate. Then it will display the information of the Sizzling Plate

Example: Name:

SizzlingPlate
Price:  50
Stocks: 5

So in here I used the preg_split to separate the 2 capital letter to have a space so in will become like this one

Example:

Name:   Sizzling Plate
Pirce:  50
Stocks: 5  

But Im encountering error Undefined offset: 2 when the added menu is just 1 word like tacos.

NOTE: Im retrieving all the datas in my menu table. My example data is SizzlingPlate. I also tried to put condition for both order page and in my controller still getting the undefined offset: 2

if($pieces[2] != '')
{
    $concat = $pieces[1] . " " .    $pieces[2];

}
else
{
    $concat = $pieces[0] . " " .    $pieces[1];
}

Question: How can I fix it?

Menu

<div class="row">
    <div class="col-md-1"></div>
    <div class="col-md-10" style="padding-left: 0px; padding-right: 0px;">
        <?php foreach($menu as $row):?>
        <img src="<?= base_url().'uploads/'.$row->menu_image?>" style="max-width: 33%">
        <?php $name = $row->menu_name; 
            $remove_space = strtolower(str_replace(' ', '', $name));?>
        <a href="order/<?= $name?>"><button class="btn btn-primary btn-md">Order Now </button></a>
        <?php endforeach;?>
    </div>
    <div class="col-md-1"></div>
</div>

When user click that menu

<?php $order_name = $name->menu_name;
                    $pieces = preg_split('/(?=[A-Z])/',$order_name);

                        $concat = $pieces[1] . " " .    $pieces[2];


                    var_dump($concat);
                    if($concat == $concat){?>

                    <div class="form-group">
                        <input type="text" disabled="" name="name" id="name" value="<?= $concat?>"">
                        <input type="hidden"  name="name" id="name" value="<?= $concat?>">
                    </div>
                <?php }?>

Controller

public function order($name)
    {
        $pieces = preg_split('/(?=[A-Z])/',$name);
                    $concat = $pieces[1] . $pieces[2];
        $where = array('menu_name' => $name);

        $get_name = $this->Crud_model->fetch_tag_row('*','menu',$where);
        parent::mainpage_order('users/order',
                [
                    'title'         =>'Order' ,
                    'name'          => $get_name
                ]
        );
    }

Edited

Question:What if I want to display it back to the original?

I've done this but it didn't work.

$uppercase_menu_name = $this->input->post('menu_name');

            $pieces = preg_replace('/(.)(?=[a-Z])/u', '$1', $uppercase_menu_name);
  • 写回答

1条回答 默认 最新

  • dongzhihong3940 2017-11-07 04:05
    关注

    If you just want to add a space between the two words, you could use preg_replace instead.

    preg_replace('/(.)(?=[A-Z])/u', '$1 ', $name);
    // Taco -> Taco
    // SizzlingPlate -> Sizzling Plate
    

    I grabbed this example from the Laravel source code: https://github.com/laravel/framework/blob/5.5/src/Illuminate/Support/Str.php#L453

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀