Array out of text in PHP [duplicate] - php

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 5 years ago.
So let's say I have a String that looks like this: "apple, banana, orange". How do i split the text to get a Array that looks like this "apple", "banana", "orange".
Thanks in advance.

Use the explode PHP function :)
<?php
$str = "apple, banana, orange";
print_r (explode(" ",$str));
?>

Related

How to convert a flat string to array not having spaces? [duplicate]

This question already has answers here:
PHP: Split string into array, like explode with no delimiter
(9 answers)
Closed 3 months ago.
I can convert a string 'html php js' to array using explode, such as
$string = 'html php js';
$output_array = explode(' ', $string);
and it works fine. But how to convert a string that has no spaces?
$string = 'html';
// *** outputs as ['h','t','m','l']
and that the output has to be an array of strings.
Try to use the str_split function: https://www.php.net/manual/en/function.str-split.php

How can we convert string into array in php? [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 1 year ago.
I have a string as mention below
$string = 20181123091338,20181130070940;
Now, I want to convert this string into an array format like
$array = array("20181123091338","20181130070940");
So, How can I do this? Please help me.
Thank You
You can use explode() .
Try this
$string = "20181123091338,20181130070940";
$arr = explode(",", $string);
echo "<pre>"; print_r($arr);
Explaination
Here we are exploding string by ","(comma), so we are passing ,(comma) as first parameter in explode function and string passing as second parameter.

Exploding a number and separate it by a space [duplicate]

This question already has answers here:
Insert string at specified position
(11 answers)
separate string in two by given position
(8 answers)
Closed 4 years ago.
I want to explode a string or an integer and separate it by a space.
E.g., I have this int 12345678, and I want its numbers to become like 123 45678. I want the first three numbers separated. Can someone give me a clue or hint in how to achieve this, like what function to use in PHP? I think using an explode will not work here because the explode function needs a separator.
You can use substr_replace() - Replace text within a portion of a string.
echo substr_replace(1234567, " ", 3, 0); // 123 4567
https://3v4l.org/9CFlX
You could use substr() :
$str = "12345678" ;
echo substr($str,0,3)." ".substr($str, 3); // "123 45678"
Also works with an integer :
$int = 12345678 ;
echo substr($int,0,3)." ".substr($int, 3); // "123 45678"
This problem will solve by using substr().
The substr() function returns a part of a string.
Syntax: substr(string,start,length)
Example:
$value = "12345678";
echo substr($value,0,3)." ".substr($value, 3);
Output: 123 45678
You may get better understand from here.

Trim Part of a string in php [duplicate]

This question already has answers here:
How to extract only part of string in PHP?
(3 answers)
Closed 5 years ago.
I have a string in a variable like this:
$var = "This is a banana (fruit)";
How do I trim / get the part 'fruit' only? (i.e., data inside braces whatever it is).
Try this:
<?php
$sentence = "This is a banana (fruit)";
$a = stripos($sentence,"(");
$b = stripos($sentence,")");
echo substr($sentence, $a, $b);
?>
Use preg_rpelace function:
<?php
$str = "This is a banana (fruit)";
echo preg_replace('/.*\((.*)\).*/','$1',$str);
?>

Split string into into array of character pairs [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP preg_split string into letter pairs
I have an string looking like this:
$str = "How are you doing?";
How can I turn this string into an array looking like this:
$arr = array("Ho","w ","ar","r ","yo","u ","do","in","g?");
$array = str_split($str, 2);
Documentation.
Use str_split() function.
Take a look at str_split(); it allows you to split a string by a definable amount of characters, so your code would look like:
$arr = str_split($str, 2);
Which will split $str into an array $arr where each element contains two characters,

Categories