preg_split() with strings containing '<' - php

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>

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.

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.

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.

Related

Add <br /> after 3rd symbol in Array

I have the following variable:
$checkbox = implode(';', $_POST['product']);
$checkbox is equal to "Product Name;Price;Unit", how can I add a break after every line?
At the moment $checkbox is equal to:
ASFP4040;18.95;1;ASFP4048;21;1;ASGS100100;25.45;1
I need it to be like:
ASFP4040;18.95;1;
ASFP4048;21;1;
ASGS100100;25.45;1;
EDIT:
I am writing this to a .TXT file, \n shows as text and doesn't actually create a new line.
As I'm not sure, how your $_POST['products'] var looks like, you might like one of these options:
If you have everything in a single array element like this
Array
(
[0] => ASFP4040
[1] => 18.95
[2] => 1
[3] => ASFP4048
[4] => 21
[5] => 1
[6] => ASGS100100
[7] => 25.45
[8] => 1
)
you could split the array into chunks and join them together
$data = implode("\n", array_map(function($chunk) {
return implode(';', $chunk);
}, array_chunk($_POST['product'], 3)));
Alternatively, if you have an array of strings like below:
Array
(
[0] => ASFP4040;18.95;1
[1] => ASFP4048;21;1
[2] => ASGS100100;25.45;1
)
a simple implode would be enough
$data = implode("\n", $_POST['product']);
Try this:
echo "'".implode("','",$checkbox)."'<br>";
You can use regular expressions to do this. Just replace my $str with your $checkbox.
$str = 'ASFP4040;18.95;1;ASFP4048;21;1;ASGS100100;25.45;1';
$str2 = preg_replace('/((?:(?:[^;]+);){3})/',"$1\n",$str);
echo $str2;
As explained in Magnus Eriksson's comment and mine, you just have to use "\n" as first parameter of your implode:
$checkbox = implode("\n", $_POST['product']);
Please notice the use of double quotes (") in order for \n to be used as a linebreak.

PHP preg_replace and explode function

I have some raw data like this
\u002522\u00253A\u002522https\u00253A\u00255C\u00252F\u00255C\
My intention is to remove the backslash "\" and first 7 digit of every string between \u002522https\ this. For this the output will be only https.
If there is only 7 digit like this \u002522\ the output will be empty.
My final intention is to put every result in a array which is formatted for the above raw data like this
Array
(
[0] =>
[1] =>
[2] => https
[3] =>
[4] =>
[5] =>
[6] =>
)
I want this result for constructing a URL. I have tried with preg_replace and explode function to get my expected result but I am failed.
$text = '\u002522\u00253A\u002522https\u00253A\u00255C\u00252F\u00255C\\';
$text = preg_replace("#(\\\\[a-z0-9]{7})#is",",",$text);
$text_array = explode(",",trim($text,'\\'));
print_r($text_array);

str_getcsv not parsing the data correctly

I have a problem with str_getcsv function for PHP.
I have this code:
<?php
$string = '#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=714000,RESOLUTION=640x480,CODECS="avc1.77.30, mp4a.40.34"';
$array = str_getcsv($string, ",", '"');
print_r($array);
Which should return:
Array
(
[0] => #EXT-X-STREAM-INF:PROGRAM-ID=1
[1] => BANDWIDTH=714000
[2] => RESOLUTION=640x480
[3] => CODECS=avc1.77.30, mp4a.40.34
)
But instead, it is returning:
Array
(
[0] => #EXT-X-STREAM-INF:PROGRAM-ID=1
[1] => BANDWIDTH=714000
[2] => RESOLUTION=640x480
[3] => CODECS="avc1.77.30
[4] => mp4a.40.34"
)
Cause it is ignoring the enclosure of the last parameter: CODECS and is spliting also that information. I'm using str_getcsv instead of just doing explode(",", $string) precisely for that reason (that function should respect the enclosure) but it is working the same as explode will do it.
The code being executed: http://eval.in/17471
The enclosure (third) parameter does not have quite that effect. The enclosure character is treated as such only when it appears next to the delimiter.
To get your desired output, the input would need to be
#EXT-X-STREAM-INF:PROGRAM-ID=1,...,"CODECS=avc1.77.30, mp4a.40.34"
See it in action.

preg_replace dont replace values

I wrote a function to strip parameters from urls, the function looks like this
function remove_it($c_link){
$regex = array();
$award = array();
$regex[] = '/[\?&](?<name>sa)=(?<value>[^&=]+)/';
$regex[] = '/[\?&](?<name>ei)=(?<value>[^&=]+)/';
$regex[] = '/[\?&](?<name>ved)=(?<value>[^&=]+)/';
$regex[] = '/[\?&](?<name>usg)=(?<value>[^&=]+)/';
foreach($regex as $remove){
$c_link = preg_replace($remove,'',$c_link);
}
return $c_link;
}
When I use a testurl like this
$test = 'http://forum.gofeminin.de/forum/dietetique/__f2955_dietetique-Diatpillen.html&sa=U&ei=8doOUa6HOsfKtAaDpICIBQ&ved=0CB0QFjAA&usg=AFQjCNEcFS48QvteNkSNcszXv5RG6VUe2g';
It's woking perfect. Now I wanted to use it in my code. So I called to function with my data and it doesn't affect the string. I used print_r to see if the string looks strange, but it's just 1:1 like in $test
$TEST-> http://forum.gofeminin.de/forum/dietetique/__f2955_dietetique-Diatpillen.html&sa=U&ei=C9wOUZuvCoeQtQavpoHoDg&ved=0CB0QFjAA&usg=AFQjCNHkRBKRpZXZX7idJ6YmSG0AIxtOdw
print_r-> http://forum.gofeminin.de/forum/dietetique/__f2955_dietetique-Diatpillen.html&sa=U&ei=C9wOUZuvCoeQtQavpoHoDg&ved=0CB0QFjAA&usg=AFQjCNHkRBKRpZXZX7idJ6YmSG0AIxtOdw
As I used all debugging methods that I know of, I don't really know where I should start searching... any pointers ?
I made antoher testrun, and saved all data in an array, later on I wanted to stript the parameter for 1 url. Here the testcode:
echo '<pre>';
print_r($test).'</br>';
echo remove_it($test[0]);
echo '</pre>';
break;
the output was like :
Array
(
[0] => http://forum.gofeminin.de/forum/dietetique/__f2955_dietetique-Diatpillen.html&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CDUQFjAA&usg=AFQjCNGgMS-nHM2JY_PkIt7C_RT2dr9bUw
[1] => http://www.fitforfun.de/abnehmen/gesund-essen/diaetpillen/diaetpillen-appetitzuegler_aid_2100.html&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CEEQFjAB&usg=AFQjCNG60KJy3wLR8DnLm9gKQEn-uR6l3w
[2] => http://www.stern.de/ernaehrung/uebergewicht-abnehmen/diaetpillen-check-welche-mittel-machen-duenn-das-abc-der-schlankmacher-615772.html&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CEYQFjAC&usg=AFQjCNGLzi5UMG4g5INDkeBdMpENgY4gHg
[3] => http://getslim.de/diaetpillen-im-test&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CEoQFjAD&usg=AFQjCNEcZnpSlVVxLgskK9DfhBF9AHGC2w
[4] => http://www.br.de/fernsehen/bayerisches-fernsehen/sendungen/gesundheit/themenuebersicht/medizin/schlankheitspillen-diaet-tabletten100.html&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CFQQFjAE&usg=AFQjCNHujKjdfNsOkarYf6MwHCPODcISjw
[5] => http://www.diaetpillenvergleich.de/beste-diatpillen/&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CFoQFjAF&usg=AFQjCNFBgbYjgutHJfp-eQztXTsKYk7rTw
[6] => http://www.diaetpillen-online.de/&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CF4QFjAG&usg=AFQjCNF083onO0rkMuQjY0tEIhhdSM4Igg
[7] => http://diaet.erdbeerlounge.de/Diaetpillen/&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CGIQFjAH&usg=AFQjCNFhNr-gsFxK1-vfjhnC1A5qQi1ZjQ
[8] => http://diaet.erdbeerlounge.de/abnehmen-forum/Diaetpillen-_t2698848s1&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CGcQFjAI&usg=AFQjCNHhHY3zUnJtwF6-HV-DbsxaVUFxsg
[9] => http://www.gutefrage.net/tag/diaetpillen/1&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CG0QFjAJ&usg=AFQjCNHPYODXZA1Sa2rs6ItnUWTOYkJj3w
)
http://forum.gofeminin.de/forum/dietetique/__f2955_dietetique-Diatpillen.html&sa=U&ei=LOIOUaqQGITntQbmmIHYBQ&ved=0CDUQFjAA&usg=AFQjCNGgMS-nHM2JY_PkIt7C_RT2dr9bUw
I made the test array and it works for me. It seems that your code is fine and something else is wrong.
Try wrapping the function input in double quotes.
remove_it("$test[0]");

Imported, Exploded Word List Doesn't Compare Properly

My original test implementation consisted of building an array of "ignore words" with the following code:
$ignoreList = array("test1", "test2", "test3");
Later on, I test for individual words in the $ignoreList:
if(in_array($word, $ignoreList)){
} else{
$words[$word] = $words[$word] + 1;
}
This code works perfectly - upon later echoing my word list, no words on the $ignoreList show up. I refactored to make it easier to add or remove words:
//Import ignore list
$ignore_raw = file_get_contents("includes/ignore.txt");
$ignoreList = explode("\n", $ignore_raw);
ignore.txt is a plain text file with each item on its own line, no spaces. The import and explode seems to be working, because a print_r statement on $ignoreList results in:
Array ( [0] => a [1] => and [2] => are [3] => as [4] => for [5] => in [6] => is [7] => more [8] => of [9] => than [10] => that [11] => the [12] => to [13] => with )
The comparison code, however, stops working properly, and words on the ignore list show up once again in my final results. Any ideas what's wrong?
Your ignore.txt file may have \r\n line endings, and your words actually have a trailing \r.
Try that:
$ignoreList = array_map('trim', file("includes/ignore.txt"));
BTW your code may be refactored like that:
$words = array_diff($words, $ignoreList); // removes ignored words
$words = array_count_values($words); // count words

Categories