2

I wants to divide the string in table and my need is

output:

// First Output
$name ='aaaa';
$first='DUT.A1';
$second='E3.3 H2.3 Y3.333 h88.h fdfd.87';
$third ='J66.H3';

// SecondOutput
$name ='bbbb';
$first='DUT.A2';
$second='F2.2 F3.1 Y1.1';
$third ='J30.A1';

Input:

$a  = "aaaa ; DUT.A1 E3.3 H2.3 Y3.333 h88.h fdfd.87 J66.H3";
$b  = "bbbb ; DUT.A2 F2.2 F3.1 Y1.1 J30.A1";
$c  = "cccc ; DUT.A3 H2.3 Y3.333 h88.h Y1.1 J45.G2";

A use explode function to divide the variable like

$exp = explode(";",$a);
$name = $exp[0];
$x = $exp[1];
$x1 = explode(" ",$x);
$total = count($x1);
$first = $x1[1];
$loop_end = $total-2;

for($i=2;$i<=$loop_end;$i++) {
    $second .= $x1[$i]."";
}
$third = $x1[$total-1]

;

Using to display Above output.

if i give,

$var  = "haha ; J1.A1 DUT.A1  DUT.A2 C1.1 C2.1 ,
              F2.1 F4.1 K1.1 ,
              F2.1 F4.1 K1.1 ,
              F2.1 F4.1 K1.1 "; 

I wants Before Semicolon as name, String like DUT.xx as first(no spaces included ex.DUT.A1 DUT.A2) The Character starts with J and After Numeric value it stored in third(ex.J1.A1 as third not J after Character). Balance Characters stored in variable Second, like (ex.

$second =  'C1.1 C2.1,F2.1 F4.1 K1.1,F2.1 F4.1 K1.1,F2.1 F4.1 K1.1';

)

1
  • 4
    First, it took me 5 minutes to reformat your question. Please use the code button in the editor, not HTML code. Second, please rephrase the question. It's hard to understand what exactly you are asking. Commented Mar 2, 2012 at 14:05

1 Answer 1

2

Is this the desired output?

$arr = array(
    "aaaa ; DUT.A1 E3.3 H2.3 Y3.333 h88.h fdfd.87 J66.H3",
    "bbbb ; DUT.A2 F2.2 F3.1 Y1.1 J30.A1",
    "cccc ; DUT.A3 H2.3 Y3.333 h88.h Y1.1 J45.G2",
    "haha ; J1.A1 J1.A2 DUT.A1  DUT.A2 C1.1 C2.1 ,F2.1 F4.1 K1.1 ,F2.1 F4.1 K1.1 ,F2.1 F4.1 K1.1 ");


foreach ($arr as $item) {
    echo "<b>item</b> = $item<hr/>";
    $parts = preg_split('/\s*;\s*/',$item);

    $name = $parts[0];
    $first = array();
    $second = array();
    $third = array();

    $split = preg_split('/\s*,\s*/',$parts[1]);

    foreach ($split as $values) {
        preg_match_all('/\b[\w\d]+\.[\d\w]+\b/',$values,$value);
        $sec = array();
        foreach ($value[0] as $item) {
            preg_match('/^DUT\./',$item,$match);
            if (!empty($match[0])) {
                $first[] = $item; continue;
            }
            preg_match('/^J\d+\./',$item,$match);
            if (!empty($match[0])) {
                $third[] = $item; continue;
            }
            $sec[] = $item;
        }
        $second[] = implode(' ', $sec);
    }

    $first = implode(' ', $first);
    $second = implode(',', $second);
    $third = implode(' ', $third);

    echo 'name = ' . $name . "\n";
    echo 'first = ' . $first . "\n";
    echo 'second = ' . $second . "\n";
    echo 'third = ' . $third . "\n\n";

}

item = aaaa ; DUT.A1 E3.3 H2.3 Y3.333 h88.h fdfd.87 J66.H3


name = aaaa
first = DUT.A1
second = E3.3 H2.3 Y3.333 h88.h fdfd.87
third = J66.H3

item = bbbb ; DUT.A2 F2.2 F3.1 Y1.1 J30.A1


name = bbbb
first = DUT.A2
second = F2.2 F3.1 Y1.1
third = J30.A1

item = cccc ; DUT.A3 H2.3 Y3.333 h88.h Y1.1 J45.G2


name = cccc
first = DUT.A3
second = H2.3 Y3.333 h88.h Y1.1
third = J45.G2

item = haha ; J1.A1 J1.A2 DUT.A1 DUT.A2 C1.1 C2.1 ,F2.1 F4.1 K1.1 ,F2.1 F4.1 K1.1 ,F2.1 F4.1 K1.1


name = haha
first = DUT.A1 DUT.A2
second = C1.1 C2.1,F2.1 F4.1 K1.1,F2.1 F4.1 K1.1,F2.1 F4.1 K1.1
third = J1.A1 J1.A2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.