php - 如何将字符串与字符和数字分开

标签 php string explode

我想划分表中的字符串,我的需要是

输出:

// 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';

输入:

$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";

使用explode函数来划分变量,如

$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]

;

用于显示以上输出。

如果我给予,

$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 "; 

我希望以分号之前作为名称,像 DUT.xx 这样的字符串作为第一个(不包含空格,例如DUT.A1 DUT.A2) 字符以 J 开头,后面是存储在第三个中的数字值(例如J1.A1 作为第三个 不是字符后面的 J)。平衡存储在变量 Second 中的字符,例如(例如

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

)

最佳答案

这是想要的输出吗?

$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

关于php - 如何将字符串与字符和数字分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9534358/

相关文章:

c++ - char s1[ ] = "xyz"和 char *s1 = "xyz"

php - 价格后分解字符串

php - 在最后出现的空格上拆分字符串

php - 为 MYSQL NOW() 设置时区

php - 获取实际 URL,此案例的最佳实践

php - Google 运算符的正则表达式

string - 如何 fmt.Printf 带有千位逗号的整数

c - 在 C 中的嵌套循环中使用 strtok() 吗?

php - json_encode 与 $_GET 结合(PHP 和 Javascript)

php - 将字符串转换为数组 - PHP