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.
Related
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
This question already has answers here:
How to use php serialize() and unserialize()
(10 answers)
Closed 5 years ago.
I want to split the string and get the specific data from this string. I am using MySQL and PHP.
I am having problems in retrieving data from database.
Here is the string:
a:6:{s:12:"cfdb7_status";s:6:"unread";s:9:"your-name";s:12:"Talha Far";s:10:"your-email";s:19:"talha4#gmail.com";s:6:"number";s:11:"03379228";s:9:"your-city";s:9:"Islamabad";s:10:"Studylevel";s:8:"Graduate";}
I want to get these values from string:
Talha Far , talha4#gmail.com , 03379228, Islamabad , Graduate
You could use unserialize() to transform the string into an object :
$str = 'a:6:{s:12:"cfdb7_status";s:6:"unread";s:9:"your-name";s:9:"Talha Far";s:10:"your-email";s:16:"talha4#gmail.com";s:6:"number";s:8:"03379228";s:9:"your-city";s:9:"Islamabad";s:10:"Studylevel";s:8:"Graduate";}';
$obj = unserialize($str) ;
var_dump($obj);
And your wanted values :
echo $obj['your-name'];
echo $obj['your-email'];
echo $obj['number'];
// ...
But, be carefull, some indices are wrong. Note the differences between your given string and the string in this anwser (ex: s:9:"Talha Far" instead of s:12:"Talha Far").
$str = 'a:6:{s:12:"cfdb7_status";s:6:"unread";s:9:"your-name";s:12:"Talha Far";s:10:"your-email";s:19:"talha4#gmail.com";s:6:"number";s:11:"03379228";s:9:"your-city";s:9:"Islamabad";s:10:"Studylevel";s:8:"Graduate";}';
$data = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $str);
print_r(unserialize($data));
You will get your data array from it and you can extract your values.
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
Having the following string:
"[{"name":null,"value":"","target":null,"alias":"","required":1,"showNull":0}]"
How can I get the 1 from the required part? I need just the 1.
Thanks in advance.
Assuming the string will always be JSON, use json_decode():
$array = json_decode('[{"name":null,"value":"","target":null,"alias":"","required":1,"showNull":0}]');
Now you can access the required option as follows:
$array[0]->required;
You can use json_decode:
<?php
$string = '[{"name":null,"value":"","target":null,"alias":"","required":1,"showNull":0}]';
$string_array = json_decode($string, true);
echo $string_array[0]['required'];
?>
In single line, using the functions json_decode and current:
$str = '[{"name":null,"value":"","target":null,"alias":"","required":1,"showNull":0}]';
$required = current(json_decode($str))->required;
This question already has answers here:
Check if String has only Integers separated by comma in PHP
(3 answers)
Closed 6 years ago.
How to check that php explode hold only comma separated integer values.
for example i have a
$str= '10,20,30,40,50,60,70,80,90,100';
$arr = explode(',',$str);
and i want to check if the value in $str is 10,20,30 and so on. and cannot contain any of string and words.
I basically populate a drop-down from this $arr and i want to make check if user enter integer values with comma then its show the dropdown else if user enter any other format.
i also use is_int or is_numeric but it cannot solve my problem.
thanks for advance .
What you're looking for is ctype_digit
$str= '10,20,30,40,50,60,70,80,90,100';
$arr = explode(',',$str);
foreach($arr as $a){
if(!ctype_digit($a)){
//Not an int
}else{
//is an int
}
}
Output:- https://eval.in/734431
Reference:- http://php.net/ctype_digit
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,