1

I have a huge string from a server, and I want each line as an object (for later foreach loop).

This is part of the string:

1535;;34290;;teaserbanner_881.jpg;;Not allowed;;closed;;;;closed;;
1535;;34291;;teaserbanner_8832.jpg;;Not allowed;;closed;;;;closed;;
1379;;31912;;teaserbanner_844.jpg;;Allowed;;open;;;;open;;
1379;;31913;;teaserbanner_8422.jpg;;allowed;;closed;;;;closed;;

The only thing that stays the same for each line is the "closing tags"
only two options:

  1. ;;closed;;;;closed;;
  2. ;;open;;;;open;;

I was thinking that it should be the needle for explode or some regex...

The final output should be:

element[0]  1535;;34290;;teaserbanner_881.jpg;;Not allowed;;closed;;;;closed;;
element[1]  1535;;34291;;teaserbanner_8832.jpg;;Not allowed;;closed;;;;closed;;
element[2]  1379;;31912;;teaserbanner_844.jpg;;Allowed;;open;;;;open;;
element[3]  1379;;31913;;teaserbanner_8422.jpg;;allowed;;closed;;;;closed;;    

The string doesn't come in "lines" it is one big line.

4

5 回答 5

1

请看一下splitsplit("\n", $string)会给你一个数组,其中每个条目是字符串的一行。

于 2013-10-02T14:43:08.947 回答
1

您可以为此使用file():-

$lines = file('path/to/file');
foreach($lines as $line){
    //do something with $line
}

$lines 是一个数组,每个元素代表文件中的一行,因此

var_dump($lines);

会给出类似的东西: -

array (size=4)
  0 => string '1535;;34290;;teaserbanner_881.jpg;;Not allowed;;closed;;;;closed;;' (length=68)
  1 => string '1535;;34291;;teaserbanner_8832.jpg;;Not allowed;;closed;;;;closed;;    ' (length=69)
  2 => string '1379;;31912;;teaserbanner_844.jpg;;Allowed;;open;;;;open;;    ' (length=60)
  3 => string '1379;;31913;;teaserbanner_8422.jpg;;allowed;;closed;;;;closed;;' length=63)
于 2013-10-02T14:43:41.047 回答
1

您可以使用preg_match_all function

$s = <<< EOF
1535;;34290;;teaserbanner_881.jpg;;Not allowed;;closed;;;;closed;;
1535;;34291;;teaserbanner_8832.jpg;;Not allowed;;closed;;;;closed;;
1379;;31912;;teaserbanner_844.jpg;;Allowed;;open;;;;open;;
1379;;31913;;teaserbanner_8422.jpg;;allowed;;closed;;;;closed;;
EOF;

if (preg_match_all('~(.*?;;(open|closed);{4}\2;;)~', $s, $arr))
   print_r($arr[1]);

输出:

Array
(
    [0] => 1535;;34290;;teaserbanner_881.jpg;;Not allowed;;closed;;;;closed;;
    [1] => 1535;;34291;;teaserbanner_8832.jpg;;Not allowed;;closed;;;;closed;;
    [2] => 1379;;31912;;teaserbanner_844.jpg;;Allowed;;open;;;;open;;
    [3] => 1379;;31913;;teaserbanner_8422.jpg;;allowed;;closed;;;;closed;;
)
于 2013-10-02T14:51:04.063 回答
1

尝试使用preg_split

$array = preg_split('/(?<=;;closed;;;;closed;;|;;open;;;;open;;)(?!$)/', $string)

(?<=;;closed;;;;closed;;|;;open;;;;open;;)确保在拆分点之前有结束标签,并(?!$)确保字符串最后没有拆分。

viper7 演示

于 2013-10-02T15:21:25.137 回答
0

巨大是什么意思?

exploding()实际上巨大的东西会耗尽你的 PHP 内存。

您需要解析它 old school,逐个字符并将它们添加到存储桶中。当您的条件得到满足时(如第 5;或第 10;或其他......),将桶视为合适的对象并处理它。但不要存储它。将其推送到文件、数据库或其他东西。

如果事情不是那么大,请使用regular expression带有“对象”格式的 a 。喜欢:

// keep duplicating the (.*?);; until you reach your number of columns.
preg_match_all '~(.*?);;(.*?);;(.*?);;(.*?);;(.*?);;~s' // pseudo-code :)

这会将其全部分解为对象和属性。您可以迭代和使用它。

于 2013-10-02T15:19:02.147 回答