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
Related
This question already has answers here:
Get the last word of a string
(10 answers)
How to get the last element of an array without deleting it?
(33 answers)
Closed 1 year ago.
How can I remove all words from a string except the last one with PHP?
For example the string "Organic Black Olives" would become "Olives".
Try this:
$str = "Organic Black Olives";
// get words list
$words = explode(' ', $str);
// get last word
$last_word = $words[count($words)-1];
echo $last_word;
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.
This question already has answers here:
How to limit the elements created by explode()
(4 answers)
Closed 7 months ago.
I have a String "Hello I am apple"
I want to split to two string
str1 = "Hello"
str2 = "I am apple"
so just simply split by the first space, then the following words will be in one string
Explode function has a 3rd parameter for that:
limit
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
http://php.net/explode
list($str1, $str2) = explode(" ", $str, 2);
Same result also can be achieved by following code....
<?php
$str = "Hello I am Apple";
$str_con = explode(" ",$str,2);
echo $str_con[0];
echo $str_con[1];
?>
This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);
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,