How to convert string to array in Laravel | Conversion in Blade file | Controller
In this article we will see how to convert a string to array in Laravel . How can you convert string to array in controller and also in your blade file in Laravel . To perform this operation we will use PHP explode() function in both our blade file as well as controller function .
Let's see both the methods that how write the code in blade file and in controller function .you can also read conversion of array to string in Laravel .
String to Array conversion in Blade file :
<!DOCTYPE html>
<html>
<head>
<title>Array to string conversion</title>
<style>
.main
{
font-size: 20px;
text-align: center;
}
</style>
</head>
<body>
<center>
<h1> LARAVEL STRING TO ARRAY CONVERSION </h1>
</center>
<div class="main">
<?php
$str = "This is an string";
echo $str."<br>";
print_r (explode(" ",$str));
?>
</div>
</body>
</html>
Output :
String to Array conversion in Controller :
checkController.php
public function demo()
{
$str="this is an array";
$result=explode(" ",$str);
return view('demo',compact('result'));
}
demo.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Array to string conversion</title>
<style>
.main
{
font-size: 20px;
text-align: center;
}
</style>
</head>
<body>
<center>
<h1> LARAVEL STRING TO ARRAY CONVERSION </h1>
</center>
<div class="main">
{{print_r($result)}}
</div>
</body>
</html>
Output :
In this way you can convert string to array in Laravel . You can write your code in both blade file as well as the controller file . Hope this article helped you.
Thank you for reading this article 😊
For any query do not hesitate to comment 💬
Also Read :
Conversion of Array to String in LaravelTypes of Migration in Laravel
How to use multiple where condition in Laravel
how to Use Laravel 7 Auth
How to make Covid-19 Tracker
How to integrate Vue JS in Laravel