1
I’m developing a CSS parser, but when it arrives in a block like this:
height: 100%;
background-image: url("data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7");
display: block;
First I have to normalize with:
$rules = str_replace(array("\n","\r"), array('',''), $rules);
// Que me retorna:
// height: 100%; background-image: url("data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"); display: block;
(because files can come with rules on the same line)
And when applying:
$rules = explode(';',$rules);
the explosion breaks in ;
inside the string "...gif;
Base64..."
I was able to "solve" by applying str_replace to ;base64
for -base64
and then when rendering the css, I replace -base64
for ;base64
. Gambiarra level 9000
This obviously limits to just this situation, and I need a broader solution, how not to break if the ;
is inside "
, '
, (
or )
I’ve tried with str_getcsv
and it doesn’t work...
Pastebin of the complete function.
In that case, I believe you can blow up in the
;\n
,– Gabriel Tadra Mainginski
@Gabrieltadramainginski I have to normalize before the explosion and remove all r n, because sometimes the file already comes with all the rules in one line...
– Jader A. Wagner
The preg_split() of the same problem?
– rray
@lost Yes, preg_split returns exactly the same thing that explodes it...
– Jader A. Wagner
Will the data before ";" always be images? (jpg, gif, etc)
– Mukotoshi
@Mukotoshi Currently only this line presented the problem, but I do not know if the customer will not use other similar lines in the future.
– Jader A. Wagner
Does it help using the Intel?
$string = explode( "\n" , $css )
and use the line$string[1] // background-image: url("data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7");
, then you can make it easier by using an ER– Papa Charlie
@Papacharlie The function is used in several different files with hundreds of CSS selectors, some already minimized and no line breaks between the rules, I can not rely on line breaking to separate the rules...
– Jader A. Wagner
It’s over 9000! I think in your case, it pays not to use explodes, but to make a small parser. I would suggest changing the title of the question to "How to use a Explode, or similar to not break enclosed delimiters", as it makes room for other solutions.
– Bacco