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.;
Related
I have this elements where I need to extract this {{search_tag}} and replace by a value
https://www.toto.com/search/10/{{search_tag}}.html#_his_
I tried this but I don't if it's the good way, does'nt work.
$words = explode('{{search_tag}} ',$website_url[$n]);
$exists_at = array_search($seach,$words);
if ($exists_at){
echo "Found at ".$exists_at." key in the \$word array";
}
You can use str_replace
$str = 'https://www.toto.com/search/10/{{search_tag}}.html#_his_';
$val = 'NEW_VALUE';
$new_str = str_replace("{{search_tag}}", $val, $str);
//outputs: https://www.toto.com/search/10/NEW_VALUE.html#_his_
How should i add the commas before,in between and after the array in php
here is my code
$arr = array(1,2,3,4,5,6,7,8,9);
$string = implode(',', $arr);
echo $string;
getting output: 1,2,3,4,5,6,7,8,9
expected output : ,1,2,3,4,5,6,7,8,9,
Just concatenate those commas
$string = ','.implode(',', $arr).',';
Another way
$arr = array(1,2,3,4,5,6,7,8,9);
$str = '';
foreach($arr as $ar){
$str .= "-{$ar}";
};
echo $str.'-';
There will be a string with a single or multiple with no commas at all.
$str = "a, b";
How do I get the first value before the comma if it contains comma(s)? This is what I did.
if(preg_match('/[,]+/', $str, $f)) {
$firstVal = $f[0];
}
NOTE: Is /^[^,]+/ better suited?
You can use strtok:
$firstVal = strtok($str, ",")
There are several ways to achieve this.
Using substr() and strpos()
echo substr($str,0,strrpos($str,','));
Using explode()
$result = explode(',',$str);
echo $result[0];
Using strstr()
echo strstr($str, ',', true);
Explode, validate and print.
$values = explode(',',$str);
if(key_exists(0,$values) && trim($values[0])!=""){
echo $values[0];
}
I have a string $string = 'contact['.'firstname'.']';
How can I get the contact and firstname from that string.
Sorry about my English, It's hard to describe the question
<?php
$string = 'contact['.'firstname'.']';
$a = explode('[',substr($string,0,-1));
echo $a[0];
echo '<br />';
echo $a[1];
?>
Use explode
$result = explode(chr(91),substr($string,0,-1));
then $result[0] and $result[1] will have what you want
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>";
}