i have a text result from database like this:
var1=value1&var2=value2&var3=value3
How to convert into a variable and get the value?
$str = "var1=value1&var2=value2&var3=value3";
parse_str($str, $output);
print_r($output);
Array
(
[var1] => value1
[var2] => value2
[var3] => value3
)
manual page:http://php.net/manual/en/function.parse-str.php
This works too:
<?php
$string = "var1=value1&var2=value2&var3=value3";
$string = explode("&", $string);
foreach($string as $key) {
$result = strrchr($key,"=");
$result = trim($result, "=");
echo $result . "<br>";
}
?>
Related
I have to extract a string like this:
index.php?module=Reports&action=abc&rname=Instantpayment
Now my task is to extract report, action and rname value in PHP.
I have tried by using explode(), but I am not able to extract module.
How can I do it?
You could use parse_str() in this case:
$string = 'index.php?module=Reports&action=abc&rname=Instantpayment';
$string = substr($string, strpos($string, '?')+1); // get the string from after the question mark until end of string
parse_str($string, $data); // use this function, stress free
echo '<pre>';
print_r($data);
Should output:
Array
(
[module] => Reports
[action] => abc
[rname] => Instantpayment
)
$yourUrl="module=Reports&action=abc&rname=Instantpayment"
$exploded_array = array();
parse_str($yourUrl, $exploded_array);
$exploded_array['module'];
$exploded_array['action'];
$exploded_array['rname'];
Use $_GET to get query strings from the URL
echo $_GET['module']; //Reports
echo $_GET['action']; // abc
echo $_GET['rname']; // Instantpayment
For getting from the string try explode():
$str ='index.php?module=Reports&action=abc&rname=Instantpayment';
$e = explode('?', $str);
$e1 = explode('&', $e[1]);
foreach($e1 as $v) {
$ex = explode('=', $v);
$newarr[$ex[0]] = $ex[1];
}
print_r($newarr); // Use this array of values you want.
//Array ( [module] => Reports [action] => abc [rname] => Instantpayment )
echo $newarr['module'];
echo $newarr['action'];
echo $newarr['rname'];
You have to access the globale GET variable:
$_GET['module']
$_GET['action']
$_GET['rname']
Try this:
<?php
$temp = "index.php?module=Reports&action=abc&rname=Instantpayment";
$t1 = explode("=",$temp);
for ($i = 1; $i < sizeof($t1); $i++)
{
$temp = explode("&", $t1[$i]);
echo $temp[0] . "\n";
}
?>
I have this data:
{"names":["George","Eric","Alice"]}
And I want to use preg_match_all to filter out the words in between quotation like this:
$s = $data;
if (preg_match_all('/"([^:]*)"/', $s, $matches)) {
echo join("\n", $matches[1]);
}
But this is outputting names George","Eric","Alice I tried many things but I cant figure it out.
* matches greedy (as much as possible). Use non-greey version: *?
if (preg_match_all('/"([^:]*?)"/', $s, $matches)) {
echo join("\n", $matches[1]);
}
output:
names
George
Eric
Alice
UPDATE
json_decode is more appropriate for this kind of work. Try following:
foreach (json_decode($s, true) as $key => $value) {
echo $key . "\n";
echo join("\n", $value);
}
This is actually JSON string use json_decode to parse it rather than using regex on this:
print_r(json_decode('{"names":["George","Eric","Alice"]}', true));
OUTPUT:
Array
(
[names] => Array
(
[0] => George
[1] => Eric
[2] => Alice
)
)
try this
$strContent = '{"names":["George","Eric","Alice"]}';
$strRegex = '%\"(.+?)\"%s';
if (preg_match_all($strRegex, $strContent, $arrMatches))
{
var_dump($arrMatches[1]);
}
Since your data is json formatted you should really treat is as json and not process it with regex which is to be used for strings. Try this:
$json = '{"names":["George","Eric","Alice"]}';
$data = json_decode($json, true);
foreach($data['names'] as $item) echo "$item\n";
Or without the hard coded "names":
$json = '{"names":["George","Eric","Alice"]}';
$data = json_decode($json, true);
foreach($data as $arr) foreach($arr as $item) echo "$item\n";
How to get each result from the below line using php?
ssrc=15012312307;themssrc=2790404163;lp=0;rxjitter=0.001079;rxcount=933;txjitter=0.000000;txcount=735;rlp=0;rtt=0.002000
I tried with explode and foreach but without success. Thanks!
Try following:
$str = "ssrc=15012312307;themssrc=2790404163;lp=0;rxjitter=0.001079;rxcount=933;txjitter=0.000000;txcount=735;rlp=0;rtt=0.002000";
$final_array = array();
$data_array = explode(';', $str);
foreach($data_array as $single_data)
{
$single_data = trim($single_data);
$single_unit = explode('=', $single_data);
$single_unit[0] = trim($single_unit[0]);
$single_unit[1] = trim($single_unit[1]);
$final_array[$single_unit[0]] = $single_unit[1];
}
print_r($final_array);
Here you will get array key as your variable name and array value as its value from cell.
$text = "ssrc=15012312307;themssrc=2790404163;lp=0;rxjitter=0.001079;rxcount=933;txjitter=0.000000;txcount=735;rlp=0;rtt=0.002000";
$exploded = explode(';', $text);
foreach($exploded as $data)
{
$temp = explode('=', $data);
$result .= 'Value of "' . $temp[0] . '" is: ' . $temp[1] . '<br>';
}
echo $result;
OUTPUT:
Value of "ssrc" is: 15012312307
Value of "themssrc" is: 2790404163
Value of "lp" is: 0
Value of "rxjitter" is: 0.001079
Value of "rxcount" is: 933
Value of "txjitter" is: 0.000000
Value of "txcount" is: 735
Value of "rlp" is: 0
Value of "rtt" is: 0.002000
You can edit this code inside foreach to your requirements. Eg. to array:
$result = Array();
foreach($exploded as $data)
{
$temp = explode('=', $data);
$result[$temp[0]] = $temp[1];
}
print_r($result);
OUTPUT:
Array
(
[ssrc] => 15012312307
[themssrc] => 2790404163
[lp] => 0
[rxjitter] => 0.001079
[rxcount] => 933
[txjitter] => 0.000000
[txcount] => 735
[rlp] => 0
[rtt] => 0.002000
)
Question
i have string like this $str="a|apple||b|bat||c|cat||d|dog";
from the above string i want to create a array dynamically n that newly created array shud look like this Array
(
[a] => apple
[b] => bat
[c] => cat
[d] => dog
)
Question
i have html string like this
$html_string="<div>Content Div1</div>
<div>Content Div2</div>
<div>Content Div3</div>";
how can i get 3rd DIV ,resulting answer should be like this
$ans="<div>Content Div3</div>" ;
Please anyone help me
for the first one
$str = "a|apple||b|bat||c|cat||d|dog";
$new_array = array();
$my_array = explode("||", $str);
$my_array = array_filter($my_array);
foreach ($my_array as $mine) {
$my = explode("|", $mine);
$new_array[$my[0]] = $my[1];
}
print_r($new_array);
// Output
Array
(
[a] => apple
[b] => bat
[c] => cat
[d] => dog
)
**for second**
$html_string = "<div>Content Div1</div><div>Content Div2</div><div>Content Div3</div>";
$new_arr = explode("</div>", $html_string);
$my_data = $new_arr[2] . '</div>';
print_r($my_data);
// Output
<div>Content Div3</div>
Try this:
First
$str = "a|apple||b|bat||c|cat||d|dog";
$my_array = explode("||", $str);
$finalArr=array();
foreach($my_array as $my_arr)
{
$myar = explode("|", $my_arr);
$finalArr[$myar[0]]=$myar[1];
}
print_r($finalArr);
For Second
$html_string="<div>Content Div1</div><div>Content Div2</div><div>Content Div3</div>";
$secondArray = explode('</div>', $html_string);
echo $res = $secondArray[2] . "</div>";
Test it on http://writecodeonline.com/php/
try this:
1st Answer:
<?php
$str="a|apple||b|bat||c|cat||d|dog";
$parentArray = explode('||', $str);
$finalArray = array();
foreach($parentArray as $parentKey=>$parentValue)
{
$childArray = explode('|', $parentValue);
$finalArray[$childArray[0]] = $childArray[1];
}
echo "<pre>";
print_r($finalArray);
?>
2nd Answer
<?php
$html_string="<div>Content Div1</div>
<div>Content Div2</div>
<div>Content Div3</div>";
$finalArray = explode('</div>', $html_string);
$resultRequired = $finalArray[2] . "</div>";
?>
For your first question:
$tmp_array = explode( '||', $str );
$str_array = array();
foreach( $tmp_array as $value ){
$tmp = explode( '|', $value );
$str_array[ $tmp[0] ] = $tmp[1];
}
For your second question
$html_array = array();
$pattern = '/\<div\>.*\<\/div\>/i';
if( preg_match_all( $pattern, $html_string, $matches ) ) {
$html_array = $matches[0];
}
Which will make:
<div>Content Div3</div>
Be in $html_array[2] if any matches are found.
I have following records in text file, need to extract that record form text file and treat them as seperate array variables
r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8) seperated by pipe(|)
I need to represent that as array use seperately like below
$r1= Array
(
[0] => 1
[1] => 2
[2] => 3
)
$r2=Array
(
[0] => 4
[1] => 5
[2] => 6
)
I have no idea how to do it, is it possible in php?
Just a plain regular expression to break up the string, followed by an explode on each group:
if (preg_match_all('#(\w+)=\(([\d,]*)\)#', $s, $matches)) {
foreach ($matches[2] as $i => $groups) {
$group_name = $matches[1][$i];
$$group_name = array_map('intval', explode(',', $groups));
}
}
print_r($r1);
print_r($r3);
print_r($rn);
You can use Eval
//Assuming you can pull the content from text file using fread
$temp = "r1=(1,2,3)|r2=(4,5,6)";
$temp=str_replace("=","=array",$temp);
$split=explode("|",$temp);
echo "<pre>";
foreach($split as $k=>$v){
$v="$".$v.";";
//Evaluate a string as PHP code .i.e You will get r1,r2 as a variable now which is array
eval($v);
}
print_r($r1);
print_r($r2);
$data = "r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$arr = explode("|", $data);
$finArray = array();
foreach($arr as $key=>$value)
{
$single = explode('(', $value);
$finArray[] = explode(',', str_replace(')', '', $single[1]));
}
print_r($finArray);
can be done as:
$string="r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$string=str_repla("r1=","",$string);
$yourArray=explode('|', $string);
This code will help you:--
<?php
$file = "/tmp/file1.txt"; // this is your file path
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
print $line;
$a=explode('|',$line);
print_r($a); // I have explode based on | for you...
foreach($a as $key=>$value)
{
print_r($value);
}
fclose($file);
}
?>
""or""
$a="r1=(1,2,3)|r2=(4,5,6)|r3=(1,2,3,4,5,7)|rn=(9,6,7,8)";
$a=explode('|',$a);
print_r($a);
<?php
$file = "file.txt";
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
$str = $line;
}
$str1 = explode("|",$str);
foreach($str1 as $temp) {
$str2 = explode("=",$temp);
$data[$str2[0]] = explode(",",trim($str2[1],"()"));
}
echo '<pre>';
print_r($data);
echo '</pre>';
?>
This will do your job.