Bạn đang xem: 7 ways to convert string to array in php
I could convert a variable to lớn string in PHP with my eyes shut. Jokes apart, PHP provides lots of pre-built function to khuyến mãi with string conversion, we are going khổng lồ use __toString() method lớn convert variable to lớn string. Let us kiểm tra out the below example:
class convertToString public $str; public function __construct($str) $this->str = $str; public function __toString() return (string) $this->str; $myObj = new convertToString("Days Gone");echo $myObj;?>// Result: Days GoneIn the above example, we have used the __toString() function khổng lồ convert variable khổng lồ string. This function doesn’t take any argument, và it is mostly used with Objects.
Let us have a look on some the string to lớn number conversion php functions with an example below.
The number_format() function in PHP is used khổng lồ convert a string into an integer.
$int = "109.126"; echo number_format($int), " "; echo number_format($int, 2); ?>// Result: 109// Result: 109.13In the next step, we’ll take help of primitive types lớn convert a string into an integer, double or float.
$number = "30061987.0915"; // int echo (int)$number, "
"; // double echo (double)$number, "
"; // float echo (float)$number, "
"; ?>// Result: 30061987// Result: 30061987.0915// Result: 30061987.0915
Xem thêm: Bà Đẻ Ăn Được Những Loại Bánh Gì Và Không Nên Ăn Những Loại Bánh Gì?
$arrray = <"star wars", "avengers", "the matrix", "ghost rider">; echo implode(" | ", $arrray);?>// star wars | avengers | the matrix | ghost rider
Using PHP’s Explode Function Let us begin with the PHP’s explode() function, this method takes 2 argument khổng lồ convert string into array. The 1st argument takes the delimiter to lớn explode the function, and the 2nd argument takes a string. Khổng lồ know more about php you can visit their official site here.
$string = "The Fountainhead, Dumbo, The Man Who Laughs, 7 Faces of Dr. Lao, Moulin Rouge"; $movies_arr = explode(", ", $string); var_dump($movies_arr);?>/* Result:array(5) <0>=> string(16) "The Fountainhead" <1>=> string(5) "Dumbo" <2>=> string(18) "The Man Who Laughs" <3>=> string(18) "7 Faces of Dr. Lao" <4>=> string(12) "Moulin Rouge"*/Using PHP’s str_split Function khổng lồ Convert String khổng lồ Array The str_split method also allows us lớn convert string to an array. The str_split function takes 2 arguments; in the first argument, we pass the string. In the second argument, we pass the number, và this number refers lớn an array element’s character length. By default, it is set lớn 1.
$string = "Loremipsumdolorsitamet"; $string_split = str_split($string, 3); var_dump($string_split);?>/* Result:array(8) <0>=> string(3) "Lor" <1>=> string(3) "emi" <2>=> string(3) "psu" <3>=> string(3) "mdo" <4>=> string(3) "lor" <5>=> string(3) "sit" <6>=> string(3) "ame" <7>=> string(1) "t"*/
The Strval Function Syntax
strval( $var )This function takes one parameter, và that is the numerical value which needs to lớn be converted to lớn string. This function returns the string value as you can see in the below example.
$var = 124.061; echo strval($var); ?> // Result: 124.061
String to Date và DateTime Conversion using Strtotime() Example:
The strtotime() Syntax The strtotime() in php function takes Time/Date and now (optional) parameter and returns the numerical value in seconds since Jan 1, 1970.
strtotime(argument);The getDate() Syntax The getDate() is useful for retrieving date/time information based on the date và time values passed into the function:
The serialize() method returns a string containing a byte-stream representation of any value that can be stored in PHP. Reference: https://www.php.net/manual/en/language.oop5.serialization.php
class NewObject public $name = "John Wick"; public function __toString() return "Movie name is: $this->name
"; $OBJECT = new NewObject; echo $OBJECT; echo serialize($OBJECT);?> /* Result: Movie name is: John WickO:9:"NewObject":1:s:4:"name";s:9:"John Wick"; */
I am Digamber, a full-stack developer và fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to lớn write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.
Twitter GitHub