Read string with name value to extract values - php

I have a string which is passed through ajax to php so that values can be read and stored in database.
The string is like:
venuetypes[]=1&venuetypes[]=2&venuetypes[]=3
How do i read this in foreach or any other way which would fetch me the values?
Please, help.

try this
$str= "venuetypes[]=1&venuetypes[]=2&venuetypes[]=3";
parse_str($str, $data);
foreach($data['venuetypes'] as $key=>$val){
}

Use parse_str:
parse_str($string, $parsed);
foreach ($parsed['venuetypes'] as $type) {
// do something with $type
}

If you are getting the value through reserved variables such as $_POST or $_GET, they are automatically parsed as an array.

Use parse_str();
$get_string = "venuetypes[]=1&venuetypes[]=2&venuetypes[]=3";
parse_str($get_string, $get_array);
print_r($get_array);`

Try this...
<?php
$yourstring = "venuetypes[]=1&venuetypes[]=2&venuetypes[]=3";
parse_str($yourstring, $array);
$key=key($array);
$count=count($array[$key]);
for($i=0;$i<$count;$i++)
{
echo $array['venuetypes'][$i];
echo "</br>";
}

Related

PHP Comma Delimiter or Explode for Foreach Loop [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 months ago.
I have a drag and drop JS plugin that saves strings as the following:
["кровать","бегемот","корм","валик","железосталь"]
I have found that I can use str_replace in an array to remove both brackets and " char. The issue that I now have is that I have a invalid argument for passing through the foreach loop as it cannot distinguish each individual word.
$bar = '["кровать","бегемот","корм","валик","железосталь"]';
$new_str = str_replace(str_split('[]"'), '', $bar);
foreach($new_str as $arr){
echo $arr;
}
So the data now outputted looks as follows (if I were to echo before the foreach loop):
кровать,бегемот,корм,валик,железосталь
Is there anyway in which I can use a comma as a delimeter to then pass this through the foreach, each word being it's own variable?
Is there an easier way to do this? Any guidance greatly appreciated!
Technically, you can use explode, but you should recognize that you're getting JSON, so you can simply do this:
$bar = '["кровать","бегемот","корм","валик","железосталь"]';
$new_str = json_decode($bar);
foreach($new_str as $arr){
echo $arr;
}
With no weird parsing of brackets, commas or anything else.
The function you need is explode(). Take a look here:
http://www.w3schools.com/php/func_string_explode.asp
Look at the following code:
$bar = '["кровать","бегемот","корм","валик","железосталь"]';
$new_str = str_replace(str_split('[]"'), '', $bar);
$exploding = explode(",", $new_str);
foreach($exploding as $token){
echo $token;
}
It looks like you've got a JSON string and want to convert it to an array. There are a few ways to do this. You could use explode
like this:
$bar = '["кровать","бегемот","корм","валик","железосталь"]';
$new_str = str_replace(str_split('[]"'), '', $bar);
$new_str_array = explode($new_str);
foreach($new_str_array as $arr){
echo $arr;
}
or you could use json_decode
like this:
$bar = '["кровать","бегемот","корм","валик","железосталь"]';
$new_str_array = json_decode($bar);
foreach($new_str_array as $arr){
echo $arr;
}
<?php
$aa = '["кровать","бегемот","корм","валик","железосталь"]';
$bb = json_decode($aa);
foreach($bb as $b)
echo $b."\n";
?>
and the results is,
кровать
бегемот
корм
валик
железосталь

PHP: Extract specific word after specific word in a string?

I have a string that looks like this:
{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}
i only need the value for the country_name from that string.
so I tried this:
$country = '{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}';
if (preg_match('#^country_name: ([^\s]+)#m', $country, $match)) {
$result = $match[1];
}
echo $result;
but there is nothing being echoed in the $result
Could someone please advise on this issue?
$country = json_decode('{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}');
echo $country->country_name;
What you have there is a JSON string.
JSON stands for JavaScript Object Notation.
PHP can decode it into an Array or Object via json_decode($string, FALSE);
The 2nd parameter by default is FALSE, which means it will convert the string into an object, which you can then access as I showed you above.
If for some reason you don't want to use JSON you can give the following a try. Note that using JSON is the recommended way doing this task.
$country = '{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}';
$temp = explode('"country_name":', $country); //Explode initial string
$temp_country = explode(',', $temp[1]); //Get only country name
$country_name = str_replace('"', ' ', $temp_country[0]); //Remove double quotes
echo $country_name;
Result:
Ireland
Try this:
<?php
$country=json_decode('{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}')
$result=$country->country_name;
echo $result;
?>
This looks like a json string. Read more about JSON.
Use it like this:
$foo = '{"ip":"XX.XX.XX","country_code":"IE","country_name":"Ireland","region_code":"L","region_name":"Leinster","city":"Dublin","zip_code":"","time_zone":"Europe/Dublin","latitude":53.333,"longitude":-6.249,"metro_code":0}';
$bar = json_decode($foo, true);
echo $bar['country_name'];
This can be used with any key from that string (eg ip, city)
More about json_decode.

Extract values from a string in php

I have a string tokenNo=12345&securityCode=111&name=Sam
How is it possible to extract the datas and store them into variables as below?
$tokenNo='12345';
$securityCode='111';
$name='Sam';
Please help
Use parse_str:
$string = 'tokenNo=12345&securityCode=111&name=Sam';
parse_str($string);
echo $tokenNo;
echo $securityCode;
echo $name;
Or you can store the entire variables in one array:
$string = 'tokenNo=12345&securityCode=111&name=Sam';
parse_str($string, $array);
print_r($array);
parse_str($string, $array);
print_r($array);
Use
parse_str()
$string = 'tokenNo=12345&securityCode=111&name=Sam';
parse_str($string);
echo $name."<br>";
echo $tokenNo."<br>";
echo $securityCode.;

php split value with "w"

I have a returning values that give me double digits, tripple digits etc. I want to put a "w" in between those numbers. Any kind of help I get on this is greatly appreciated.
$value = "444";
I want it to give me back this:
$value = "4w4w4w";
You could convert the string into an array and them implode it:
echo implode("w", str_split("444")) . "w";
Lets say $inp holds your number.
$arr = str_split($inp);
$result = implode('w',$arr);
str_split() and implode() will help.
$var = "444";
$array = str_split($var);
$final = implode("w", $array);
In this case you $final = "4w4w4";, so you might want to append an extra "w" to the end.
Here's a short way to do it with regex:
$str = '444';
echo preg_replace ( '/(.)/', '$1w', $str );
try this
$value="444";
$out='';
for($i=0;$i<strlen($value);$i++){
$out.=$value[$i]. "w";
}
echo $out;
output
4w4w4w

How can I split a string?

i hv a string like
$q ="menu=true&submenu=true&pcode=123456&code=123456" ;
i want to get the value of pcode = 123456 and code = 123456
how i can get...?
Use parse_str function if it's not from url (then use $_GET array)
http://ru2.php.net/manual/en/function.parse-str.php
Use explode to get array from a string.
explode('&',$q);
It will explode string on every & character and return pieces in an array.
See parse_str
$q ="menu=true&submenu=true&pcode=123456&code=123456" ;
// parse str values into an array called $pairs
parse_str($q, $pairs);
// loop through $pairs and display all values
foreach ($pairs as $key => $val) {
echo "$key => $val\n";
}
Please use php explode functions to do it
ex:
<?php
$q ="menu=true&submenu=true&pcode=123456&code=123456" ;
$pieces = explode("&",$q);
print_r($pieces);
foreach($pieces as $key=>$value){
$newVarible[]=explode("=",$value);
}
print_r($newVarible);
?>

Categories