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;
Related
This question already has answers here:
How substr function works? [closed]
(3 answers)
Closed 4 years ago.
I am trying to get "pa" from "a(bcdefghijkl(mno)pa)q".
This is my code for exampe:
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,15);
echo $mystring;
outputs is:
mno)pa)q
You have use right code but the second parameter is wrong. use this one below
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,2);
echo $mystring;
In substr function:
first parameter means from which position of string starts.
and the second parameter means how many characters you have want.
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,-4,2);
echo $mystring;
Try this
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 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 answers here:
PHP JSON encode. Echo value [duplicate]
(3 answers)
Closed 5 years ago.
I have this string
{"CALL CENTER":"CALL CENTER"}
I need to print CALL CENTER and I have tried using
substr($mystring, strpos($mystring, ":") + 1)
It gives me "CALL CENTER"}
How can I remove the special character from the result?
Use json_decode with the assoc array flag set to true and then you can just access the data in the array instead of parsing the string yourself.
$jsonArray = json_decode('{"CALL CENTER":"CALL CENTER"}', true);
echo $jsonArray['CALL CENTER']; // CALL CENTER
$jsonArray = json_decode('{"CALL CENTER":"CALL CENTER 2"}', true);
echo $jsonArray['CALL CENTER']; // CALL CENTER 2
http://sandbox.onlinephpfunctions.com/code/7650e1b9e705318e31c2b02f44ed05f9ee201d13
You can use the trim() function:
$mystring = trim($mystring, '"}');
Documentation: http://php.net/manual/en/function.trim.php
This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 8 years ago.
$string = "comment-454";
I want to disply only 454? not comment-
i am saving in db comment-454 , now i want to crop comment-.. how can i
can someone provide me something, because i am newbie in php..
thanks in advance..
$string = str_replace("comment-", "", "comment-454");
should replace what you want.
you mentioned comment-454 came from your database. So ill assume it's in $result.
Now you can do:
$string = str_replace("comment-", "", $result);
echo $string;
Use explote() http://php.net/manual/de/function.explode.php
$teile = explode("-", $string);
echo $teile[1]; // 454