i want to get specific data from a string [duplicate] - 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.

Related

PHP implode and explode functions [duplicate]

This question already has answers here:
implode() string, but also append the glue at the end
(6 answers)
Closed 1 year ago.
I have this sentence:
piece:5,dozen:10
and I need to print it via php like this:
piece:5$,dozen:10$
without editing the line in the editor, I need php to do it automatically. Now I thought to split it like this:
$uom = "piece:5,dozen:10";
$lst1 = explode(',', $uom);
var_dump($lst1);
and it returned like this:
array (size=2)
0 => string 'piece:5' (length=7)
1 => string 'dozen:10' (length=8)
which is ok for now, so I need after each string piece:5$ dozen:10$ and then I did this:
$lst2 = implode('$ ', $lst1);
echo $lst2;
It printed:
piece:5$ dozen:10
I need the $ to be printed also after 10, so it will be like this:
piece:5$ dozen:10$
What is the right way to do this? What did I do wrong? What if the values are dynamically coming from the database?
You can use a combination of explode, array_map and implode like so:
<?php
$uom = 'piece:5,dozen:10';
$add_dollars = array_map(function($e) {
return $e . '$';
}, explode(',', $uom));
echo implode(" ", $add_dollars);
I see few explode-implode answers. I don't want to duplicate answer, so let me do it another way – with regex.
$data = "piece:5,dozen:10";
echo preg_replace("/(\d+)/i", "$1\$", $data);
It may be a good idea to make it a bit more complex, i.e. take not only \d+, but also previous string and colon. Rather without(!) comma. In my opinion this may be better (because it's usually worth to be strict in regex):
$data = "piece:5,dozen:10";
echo preg_replace("/([a-zA-Z]:\d+)/i", "$1\$", $data);
I encourage you to read php manual: preg_replace.
Answering also the question about jQuery – you don't need to use jQuery, you can do that in pure javascript. And it will be really similar! For example:
let data = "piece:5,dozen:10";
let modified = data.replace(/([a-zAZ]:\d+)/g,"$1\$");
console.log(modified);

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.

Getting a part of a string in PHP [duplicate]

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;

php parsing a basic string contained in a variable [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 7 years ago.
I'm new to php and have a basic question regarding parsing strings.
I have a variable "SKU" whose value is "9897_BLK"
I need to split this into two separate values:
"STYLE" with a value of "9897" AND "COLOR" with a value of "BLK"
I suppose there's a way to use the underscore to delimit the string.
Thanks for your help.
Try explode. This basically separates your string using a delimiter as your first parameter, and the string as the second parameter and returns an array of strings generated. Afterwards you can check if the string was properly parsed or you can just directly assign values just like the one below:
$sku = "9897_BLK";
$sku_parsed = explode("_", $sku);
$style = $sku_parsed[0];
$color = $sku_parsed[1];
If you want more details, the PHP manual is very accessible and has in-depth examples and use-cases for various scenario.
http://php.net/manual/en/function.explode.php
Try this:
$sku = "9897_BLK";
list($style, $color) = explode("_", trim($sku));
In PHP we use an function to split any string with delimiter.
You may try the explode function. The explode function return an array as output and the array values are the delimited values respectively.
Here is code snippet:
$SKU = "9897_BLK";
$DELIMITED_ARRAY = explode("_", $SKU);
$STYLE = $DELIMITED_ARRAY[0];
$COLOR = $DELIMITED_ARRAY[1];

Categories