PHP爆炸字符串

PHP爆炸字符串,php,explode,Php,Explode,我有这个字符串:0 | dy1497oryoldy932oslcph | 1 | 0 | 0 我要把它炸成这样: 0 | DY1497 ORY OSL DY932 OSL CPH | 1 | 0 | 0 我搜索过了,但我能找到的只是当文本以/、-等分隔时如何分解。有什么想法吗?最好的选择可能是正则表达式: if (preg_match('/|(.{6})(.{3})(.{3})(.{5})(.{3})(.{3})|/', $string, $matches)) { echo $match

我有这个字符串:0 | dy1497oryoldy932oslcph | 1 | 0 | 0

我要把它炸成这样:

0 | DY1497 ORY OSL DY932 OSL CPH | 1 | 0 | 0


我搜索过了,但我能找到的只是当文本以/、-等分隔时如何分解。有什么想法吗?

最好的选择可能是正则表达式:

if (preg_match('/|(.{6})(.{3})(.{3})(.{5})(.{3})(.{3})|/', $string, $matches)) {
    echo $matches[1];
    echo $matches[2];
    echo $matches[3];
    echo $matches[4];
    echo $matches[5];
    echo $matches[6];
}

这将字符串简单地除以字符长度。您可能需要根据需要对此进行修改。请参见

如果您知道所需字符的确切位置,我建议使用substr

首先,您必须使用一个特殊的字符,如-、$等

然后你可以引爆它

之后,您可以在php中使用以下语法来分解字符串。。。。。。

要查看您的结果:--


我希望它能帮助您。

另一种拆分由固定长度字段组成的字符串的方法是使用“unpack”,它非常适合此工作

下面是一些经过测试的代码,演示了这一点:

<?php

$origSource = '0|DY1497ORYOSLDY932OSLCPH|1|0|0';

// expected result
$string1 = 'DY1497';
$string2 = 'ORY';
$string3 = 'OSL';
$string4 = 'DY932';
$string5 = 'OSL';
$string6 = 'CPH';

// get the string we are interested in...
$tempStr = explode('|', $origSource);

$source = $tempStr[1]; // string with fixed length fields
/* debug */ var_dump($source);

// define where the fixed length fields are and the indexes to call them in the output array
$format = 'A6field1/' . 'A3field2/' . 'A3field3/'. 'A5field4/'. 'A3field5/'. 'A*field6/' ; // make it clear where the fields are...

$dest = unpack($format, $source);
/* debug */ var_dump($dest);

?>

儿子,在我看来,你好像在这个字符串中搜索一些特定的标记。我想你能做的是,把你自己的一个数组变成一个小搜索模式,你可能会成为一个搜索者。这里是这样的:

$patterns = array('ORY', 'OSL', 'CPH', 'DY[\d]+');
然后,您可以将yerself制作成一个漂亮的大型regexp输出:

$regexp = '/(' . implode('|', $patterns) . ')/';
然后,你要做的是,用preg_match_all找到所有这些家伙:

preg_match_all($regexp, $string, $matches);

现在,你这样做,然后看看你在$matches中得到了什么。我敢打赌它有你需要的东西

模式是什么?嗯,字符串看起来没有一个共同的模式,即使是正则表达式。你有没有像@Babyazerty所要求的那样将字符串分开的模式?字符串1=5,字符串2=3的数字总是相同的?你有没有尝试过$resultArray=explode |,0 | dy1497或osldy932oslcph | 1 | 0来去除周围的数字?我是用Symfony2 DOMCrawler从其他网站得到这个字符串的。现在我需要分解它并在下一个操作中使用它。这一点都没有帮助。看起来很棒,但每次我选择其他选项时,我的字符串都会动态变化。符号的数量也可能会改变。@此外,您只需根据需要调整正则表达式模式即可。看起来很棒,但每次选择其他选项时,我的字符串都会动态变化。符号的数量也可能会改变。
$patterns = array('ORY', 'OSL', 'CPH', 'DY[\d]+');
$regexp = '/(' . implode('|', $patterns) . ')/';
preg_match_all($regexp, $string, $matches);