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);
?>
Related
I have a string of this type:
string(11) "2=OK, 3=OK"
from a text file. But I would like to convert it into an array of keys this type :
array (
[2] => Ok
[3] => Ok
)
I was wondering how we could do that in PHP.
Note:- I normally use explode() and str_split() for the conversions string into array but in this case I don't know how to do it.
use explode(), foreach() along with trim()
<?php
$string = "2=OK, 3=OK" ;
$array = explode(',',$string);
$finalArray = array();
foreach($array as $arr){
$explodedString = explode('=',trim($arr));
$finalArray[$explodedString[0]] = $explodedString[1];
}
print_r($finalArray);
https://3v4l.org/ZsNY8
Explode the string by ',' symbol. You will get an array like ['2=OK', ' 3=OK']
Using foreach trim and explode each element by '=' symbol
You can use default file reading code and traverse it to achieve what you want,
$temp = [];
if ($fh = fopen('demo.txt', 'r')) {
while (!feof($fh)) {
$temp[] = fgets($fh);
}
fclose($fh);
}
array_walk($temp, function($item) use(&$r){ // & to change in address
$r = array_map('trim',explode(',', $item)); // `,` explode
array_walk($r, function(&$item1){
$item1 = explode("=",$item1); // `=` explode
});
});
$r = array_column($r,1,0);
print_r($r);
array_walk — Apply a user supplied function to every member of an array
array_map — Applies the callback to the elements of the given arrays
explode — Split a string by a string
Demo.
You can use preg_match_all along with array_combine, str_word_count
$string = "2=OK, 3=OK" ;
preg_match_all('!\d+!', $string, $matches);
$res = array_combine($matches[0], str_word_count($string, 1));
Output
echo '<pre>';
print_r($res);
Array
(
[2] => OK
[3] => OK
)
LIVE DEMO
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>";
}
Array {
[0] => http://abc.com/video/ghgh23;
[1] => http://smtech.com/file/mwerq2;
}
I want to replace the content between /sometext/ from the above array. Like I want to replace video, file with abc.
You don't need to loop over every element of the array, str_replace can take an array to replace with:
$myArray = str_replace(array('/video/', '/file/'), '/abc/', $myArray);
However, based on your question, you might want to replace the first path segment, and not a specific index. So to do that:
$myArray = preg_replace('((?<!/)/([^/]+)/)', '/abc/', $myArray);
That will replace the first path element of every URL in $myArray with /abc/...
One way is to use str_replace()
You can check it out here: http://php.net/str_replace
Either str_replace as other comments suggested or using a regular expression especially if you might have a longer url with more segments like http://example.com/xxx/somestuff/morestuff
In that case str_replace will not be enough you will need preg_replace
This is another option. Supply a array, foreach will pick it up and then the first parameter of str_replace can be a array if needed. Hope you find this helpful.
<?php
$array = array('http://abc.com/video/ghgh23','http://smtech.com/file/mwerq2');
$newarray = array();
foreach($array as $url) {
$newarray[] = str_replace(array('video','file'),'abc',$url);
}
print_r($newarray);
?>
//every element in $myArray
for($i=0; $i < count($myArray); $i++){
$myArray[$i] = str_replace('/video/','/abc/',$myArray[$i]);
}
$array = array('http://abc.com/video/ghgh23', 'http://smtech.com/file/mwerq2');
foreach ($array as &$string)
{
$string = str_replace('video', 'abc', $string);
$string = str_replace('file', 'abc', $string);
}
hey there I have this,
$following_user_id .= $row['following_user_id'];
and I get
44443344330
then I use the implode() function and seperate with commans
44,44,33,44,33,0,
but I don't want the last comma on the last number?
Is this possible?
$following_user_ids = array();
//loop this:
$following_user_ids[] = $row['following_user_id'];
$user_ids_string = implode(',',$following_user_ids);
You can split the string into an array of characters, then implode the array.
$array = preg_split('//', $following_user_id, -1, PREG_SPLIT_NO_EMPTY);
echo implode( ',', $array );
Collect your data into an array of strings and use the implode function:
$uids = array();
while($row = mysql_fetch_assoc($result)){
array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);
Check implode: http://php.net/manual/en/function.implode.php
Code example: I'm assuming your using some sort of loop?
$arrUsers = new array();
... your loop code here ...
array_push($arrUsers, $row['following_user_id']);
... end loop code ..
$following_user_id = impload(",", $arrUsers);
Implode should not be inserting a comma at the end of that string there. Are you sure there isn't an empty string at the end of your array sequence?
Either way, to fix the string you have, just get rid of the last character of the string:
$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);
Further, if you're not going to be using the non-comma delimited number set, why don't you just add a comma every time you add a user id. That way you don't even have to use the implode function.
This works for me:
<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>
Try using arrays, example
<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';
echo implode(',', $arr);
hey there I have this,
$following_user_id .= $row['following_user_id'];
and I get
44443344330
then I use the implode() function and seperate with commans
44,44,33,44,33,0,
but I don't want the last comma on the last number?
Is this possible?
$following_user_ids = array();
//loop this:
$following_user_ids[] = $row['following_user_id'];
$user_ids_string = implode(',',$following_user_ids);
You can split the string into an array of characters, then implode the array.
$array = preg_split('//', $following_user_id, -1, PREG_SPLIT_NO_EMPTY);
echo implode( ',', $array );
Collect your data into an array of strings and use the implode function:
$uids = array();
while($row = mysql_fetch_assoc($result)){
array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);
Check implode: http://php.net/manual/en/function.implode.php
Code example: I'm assuming your using some sort of loop?
$arrUsers = new array();
... your loop code here ...
array_push($arrUsers, $row['following_user_id']);
... end loop code ..
$following_user_id = impload(",", $arrUsers);
Implode should not be inserting a comma at the end of that string there. Are you sure there isn't an empty string at the end of your array sequence?
Either way, to fix the string you have, just get rid of the last character of the string:
$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);
Further, if you're not going to be using the non-comma delimited number set, why don't you just add a comma every time you add a user id. That way you don't even have to use the implode function.
This works for me:
<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>
Try using arrays, example
<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';
echo implode(',', $arr);