如何在 Laravel 中使用 php 爆炸?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42017587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:15:15  来源:igfitidea点击:

how to use php explode in laravel?

laravel

提问by TheBAST

 $user = $this->user;
            $user->name = $request['name'];
            $user->email = $request['email'];
            $user->password = $request['password'];
            $user->save();

$name = explode(' ' ,$user->name);

$profile= $user->userdetail()->create([
                'user_id' => $request->input('id'),
                'first_name' => <the value of the first exploded string>
'last_name' => the value of the secondexploded string
                ]);

            return redirect('confirmation');
        }

how to split two words using explode function in php? For example i registered wth name of JOhn doe I want to create in my userdetail table the first_name of john and last_name of doe. How can i do it/

如何在php中使用explode函数拆分两个单词?例如,我注册了 JOhn doe 的名字,我想在我的 userdetail 表中创建 john 的 first_name 和 doe 的 last_name。我该怎么做/

采纳答案by Alexey Mezenin

explode()returns an array of strings, so you can access elements by using keys:

explode()返回一个字符串数组,因此您可以使用键访问元素:

$profile = $user->userdetail()->create([
              'user_id' => $request->input('id'),
              'first_name' => $name[0],
              'last_name' => $name[1]
          ]);

回答by Manvir Singh

You can use the following code

您可以使用以下代码

$full_name = "John Doe";
$name = explode(' ',$full_name);
$first_name = $name[0];
$last_name = $name[1];

回答by Dee

Alternate way :

替代方式:

<?php

   $ip = "fname lname"; 
   $iparr = preg_split("/[\s,]+/",$ip); 
   echo "$iparr[0]";
   echo "$iparr[1]";

?>

But explode is good(faster) than preg_split. ;)

但是爆炸比 preg_split 好(更快)。;)