Created
May 9, 2018 14:17
-
-
Save enniosousa/a86b567ed24da05ecc638be8baacb6b2 to your computer and use it in GitHub Desktop.
Explode string to array scaping literal char
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//code from https://stackoverflow.com/a/38071156/4830771 | |
function separate($string, $separator = '|', $escape = '\\') { | |
if (strlen($separator) != 1 || strlen($escape) != 1) { | |
trigger_error(__FUNCTION__ . ' requires delimiters to be single characters.', E_USER_WARNING); | |
return; | |
} | |
$segments = []; | |
$string = (string) $string; | |
do { | |
$segment = ''; | |
do { | |
$segment_length = strcspn($string, "$separator$escape"); | |
if ($segment_length) { | |
$segment .= substr($string, 0, $segment_length); | |
} | |
if (strlen($string) <= $segment_length) { | |
$string = null; | |
break; | |
} | |
if ($escaped = $string[$segment_length] == $escape) { | |
$segment .= (string) substr($string, ++$segment_length, 1); | |
} | |
$string = (string) substr($string, ++$segment_length); | |
} while ($escaped); | |
$segments[] = $segment; | |
} while ($string !== null); | |
return $segments; | |
} | |
$string = <<<DATA | |
1307|15|1|0|0|1429624850|0|1|0|0.00|0|0||114|0|Aproveite nossas SUPER OFERTAS!!!||<p><span style="font-size:16px;"><span style="color: rgb(20, 24, 35); font-family: helvetica, arial, 'lucida grande', sans-serif; line-height: 18px;">Aproveite nossas SUPER OFERTAS!!! Colocação de Mega-Hair ou Alongamento por APENAS R$ 69,99!!!!!</span><br style="color: rgb(20, 24, 35); font-family: helvetica, arial, 'lucida grande', sans-serif; line-height: 18px;" /><br style="color: rgb(20, 24, 35); font-family: helvetica, arial, 'lucida grande', sans-serif; line-height: 18px;" /><a href="http://wwwj.mp/karlacabelos" rel="nofollow nofollow" style="color: rgb(59, 89, 152); cursor: pointer; text-decoration: none; font-family: helvetica, arial, 'lucida grande', sans-serif; line-height: 18px;" target="_blank">wwwj.mp/karlacabelos</a><br style="color: rgb(20, 24, 35); font-family: helvetica, arial, 'lucida grande', sans-serif; line-height: 18px;" /><strong><span style="color: rgb(20, 24, 35); font-family: helvetica, arial, 'lucida grande', sans-serif; line-height: 18px;">Feira de Santana - Ba</span></strong></span></p>||0||0|karlacabelosok|||||||||||0|||36|0|90734536`jpg`960`960`500`500\||aproveite_nossas_super_ofertas|1429624910|0|0 | |
DATA; | |
print_r(separate($string, $separator = '|', $escape = '\\')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment