1

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?

6 Answers 6

1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

good one , can it be edited to make those r1,r2 and r3 as seperate variable , i am just curious
@timus2001 I've added a second part to the answer that also extracts the group name. Is that what you meant?
still no :) , (s)he need all arrays as seperate variable like $r1,$r2.. small eval is needed ... i got the solution but if some line added to your code its even more generic
its really awesome, hats off logic :)
thank you sir, it worked . But as a beginner i found regular expression tough
|
0

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);

2 Comments

amazing! now i can acess all those r1,r2,r3 as $r1,$r2,$r3. Thank you
go with jack's logic its even better
0
$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);

Comments

0

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);

Comments

0

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);

4 Comments

@timus2001 i am just giving rough idea how to do that.. not doing full code .. understand..
(s)he need $r1,$r2 ,$rn as seperate array :)
hmm the logic is we need to change each element of array as one seperate array , please check Eval function in php
thank you for your effort sir though its not what i need, timus2001 is 100% correct
0
    <?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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.