如何在 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
how to use php explode in 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
回答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 好(更快)。;)