0

I parsing a license key list in php. Unfortantly the results are not like expected. It seems that the problem occurs by the special character '<'. Would be nice if somebody have an idea of possible solutions.

$file_content = '
HM$WN*G&Z58CY8FPUA
F*QZHZGK#&*@*492&T
JJKXP<GZRPKGS7J!EW
P8ZHZ<GCNNR6X=Z7PW
C6HXQFGJ*Y2+#SDZT9
BYYYMEGMQ73G5K#U7F
P>+F=GG7F*U#<RT!6H
B+ZZYTGX&LF6@6XUXU
X&PHNAGN+X><NZYN#9';

$file_array = preg_split("/\n/", $file_content);

echo '<pre>';
print_r($file_array);

OUTPUT

[0] => 
[1] => HM$WN*G&Z58CY8FPUA
[2] => F*QZHZGK#&*@*492&T
[3] => JJKXP P8ZHZ C6HXQFGJ*Y2+#SDZT9
[6] => BYYYMEGMQ73G5K#U7F
[7] => P>+F=GG7F*U# B+ZZYTGX&LF6@6XUXU
[9] => X&PHNAGN+X>
mcxbain
  • 89
  • 5
  • 2
    Are you sure there is a problem, have you checked the source? If you are using a browser to look at the results, the browser probably thinks it is an opening html tag. – jeroen May 12 '16 at 07:04

3 Answers3

0

There's no benefit from using preg_split() in your case. Use

$file_array = explode("\n", $file_content);

instead, or if the content is being read from file, just do

$file_array = file($filename, FILE_IGNORE_NEW_LINES);

for the same result.

EDIT

but the result should be look like this

If you mean the output is the same as from your question - yes it is, because it is the correct output. The problem is that you are viewing this in web browser which then may consider <... as part of HTML markup. Add echo '<pre>'; before your print_r() to prevent this or run your script in console.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

Your split works as it should be, only thing is that browser converts those symbols to tags and causing that. You can check that by running this (I used htmlentities):

<?php
$file_content = '
HM$WN*G&Z58CY8FPUA
F*QZHZGK#&*@*492&T
JJKXP<GZRPKGS7J!EW
P8ZHZ<GCNNR6X=Z7PW
C6HXQFGJ*Y2+#SDZT9
BYYYMEGMQ73G5K#U7F
P>+F=GG7F*U#<RT!6H
B+ZZYTGX&LF6@6XUXU
X&PHNAGN+X><NZYN#9';

$file_array = preg_split("/\n/", $file_content);

array_map("HTMLescape", $file_array);

function HTMLescape($a) {
    echo "<pre>".htmlentities($a)."</pre>";    
}

Output:

HM$WN*G&Z58CY8FPUA
F*QZHZGK#&*@*492&T
JJKXP<GZRPKGS7J!EW
P8ZHZ<GCNNR6X=Z7PW
C6HXQFGJ*Y2+#SDZT9
BYYYMEGMQ73G5K#U7F
P>+F=GG7F*U#<RT!6H
B+ZZYTGX&LF6@6XUXU
X&PHNAGN+X><NZYN#9

Moreover, for just splitting this line, as @Marcin Orlowski pointed, you can opt for explode which is faster.

Community
  • 1
  • 1
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
0

Your code is working fine with some minor modifications:

<?php

$file_content = <<<'EOT'
HM$WN*G&Z58CY8FPUA
F*QZHZGK#&*@*492&T
JJKXP<GZRPKGS7J!EW
P8ZHZ<GCNNR6X=Z7PW
C6HXQFGJ*Y2+#SDZT9
BYYYMEGMQ73G5K#U7F
P>+F=GG7F*U#<RT!6H
B+ZZYTGX&LF6@6XUXU
X&PHNAGN+X><NZYN#9
EOT;

$file_array = preg_split("/\n/", $file_content);

print_r($file_array);

The obvious output on php cli is:

Array
(
    [0] => HM$WN*G&Z58CY8FPUA
    [1] => F*QZHZGK#&*@*492&T
    [2] => JJKXP<GZRPKGS7J!EW
    [3] => P8ZHZ<GCNNR6X=Z7PW
    [4] => C6HXQFGJ*Y2+#SDZT9
    [5] => BYYYMEGMQ73G5K#U7F
    [6] => P>+F=GG7F*U#<RT!6H
    [7] => B+ZZYTGX&LF6@6XUXU
    [8] => X&PHNAGN+X><NZYN#9
)

Note that to visualize that result in a html displaying browser you have to escape the html specific characters (like < and >). but that has nothing to do with splitting the input string, what your question is about.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Though you use heredoc, browsers automatically parse HTML tags – Thamilhan May 12 '16 at 07:13
  • @Thamilan Sorry, can't follow there. First, that is _not_ `heredoc`, but a `nowdoc` definition I use. Second, that string definition has nothing to do with a browser at all. It defines a string on the server side, no browser is involved. – arkascha May 12 '16 at 07:15
  • Yes but while the text is displayed in browser, it will certainly process open and close tags before printing, however, the OP's concept will work in terminal or in console – Thamilhan May 12 '16 at 07:17
  • @Thamilan Which is exactly what I wrote in the answer, if you read it. – arkascha May 12 '16 at 07:18
  • Extremely sorry about my mistake. Forgive! Read lately. [Better to remove these conversation as it is of no use for future readers] agree? – Thamilhan May 12 '16 at 07:20