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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了