I'm creating a CSS generator in PHP which uses CSSX (yep, they are my idea) files (with a special syntax). One feature is 'short comments':
body
{
font-family: Georgia; //I really like this font!
}
Now I want to replace this comment with a /* ... */ comment, so the output is like this:
body
{
font-family: Georgia; /*I really like this font!*/
}
How can I do this? Thanks,
P.S. The complete CSSX file is read into one string variable.
P.P.S This q is answered. To fix the url('//server/etc.cssx') problem, use this:
$file = preg_replace('~[^"\'\(]//([^\r\n]*)[^"\'\)]~', '/*$1*/', $file);
A regexp should do the trick:
$str = preg_replace('_//(.*)$_m', '/*$1*/', $str);
This won't take into account quoted strings - if you're using something crazy like
background-image: url('//my-server/my.jpg');
then it's going to think that's a comment.
If this is a problem then you're better off writing a proper parser.
<? preg_replace('#//(.*)$#', '/*$1*/', $cssx); ?>
Greg's expression has two problems: first, 'm' and '$' are superfluous, second it doesn't handle carriage returns correctly (in case your system uses them).
A better expression appears to be
preg_replace('~//([^\r\n]*)~', '/*$1*/', $str);
Related
I would like to know why the function addcslashes() is ignoring certain characters.
As you will notice in the output at the bottom, ["`","$","""] are being ignored.
This is my example:
<?php
$ADPasswdRaw = $_GET["element_3"]; #data from a web form
$ADPasswd = addcslashes($ADPasswdRaw, "~`!##$%^&*()_+=-][}{\\|:;\"',./<>?");
echo $ADPasswd;
?>
Output
\~\`\!\#\\\#$\%\^\&\*\(\)\_\+\-\=\;\:"\'\<\>\?\,\.\/
Thanks
This must be a problem with my input.
This is unclear and old at this point.
Is there a quick and easy way to fix HTML tags that are misplaced, in a web document? Such as:
<strong><span style="border:1px;">Text</strong></span>
/\ /\
|______________________________________|
So that it looks like:
<strong><span style="border:1px;">Text</span></strong>
Edit: you are suggesting HTML fixers, but what I'm looking for is a function type solution. Would it help if you could consider this to be BBcode? [b][u]Text[\b][\u]
I think the best solution is using Html Purifier, works pretty good:
Demo: http://htmlpurifier.org/demo.php
Works with your input perfectly.
How should a computer know whether you meant for the span to be inside the strong, or the other way around?
The "quick and easy way" is to run your document through an HTML validator, then fix the issues that it identifies using your noggin and keyboard.
You can use tidy::repairFile() or tidy::repairString(), but repairing is not straightforward, so you can never be sure the result will be what you expect. Example from the documentation:
<?php
$file = 'file.html';
$tidy = new tidy();
$repaired = $tidy->repairfile($file);
rename($file, $file . '.bak');
file_put_contents($file, $repaired);
?>
I'm away from a computer with PHP installed and was wondering what the result of strip_tags() would be on the following text:
"<scr<h1>ipt>alert('oh oh')</scr</h1>ipt>"
Would it return:
"<script>alert('oh oh')</script>" (i.e. not recognize that by removing the obvious tag it exposed a new one)
or
"alert('oh oh')
I know that if it returns the first case I can just repeatedly call the function until I get out what I put in, but I'm curious.
Thanks in advance.
Great question.
Nope, it doesn't strip anything from that string:
<?php
$b = "ipt>alert('oh oh')ipt>";
echo strip_tags($b);
?>
And the output is your original string: ipt>alert('oh oh')ipt>
Edit
In your second case it will print alert('oh oh') so it strips all that is looking like a tag in a single step
It returns just:
alert('oh oh')
I was creating a Syntax Highlighter in PHP but I was failed! You see when I was creating script comments (//) Syntax Highlighting (gray) , I was facing some problems. So I just created a shortened version of my Syntax Highlighting Function to show you all my problem. See whenever a PHP variable ,i.e., $example, is inserted in between the comment it doesn't get grayed as it should be according to my Syntax Highlighter. You see I'm using preg_replace() to achieve this. But the regex of it which I'm using currently doesn't seem to be right. I tried out almost everything that I know about it, but it doesn't work. See the demo code below.
Problem Demo Code
<?php
$str = '
<?php
//This is a php comment $test and resulted bad!
$text_cool++;
?>
';
$result = str_replace(array('<','>','/'),array('[',']','%%'),$str);
$result = preg_replace("/%%%%(.*?)(?=(\n))/","<span style=\"color:gray;\">$0</span>",$result);
$result = preg_replace("/(?<!\"|'|%%%%\w\s\t)[\$](?!\()(.*?)(?=(\W))/","<span style=\"color:green;\">$0</span>",$result);
$result = str_replace(array('[',']','%%'),array('<','>','/'),$result);
$resultArray = explode("\n",$result);
foreach ($resultArray as $i) {
echo $i.'</br>';
}
?>
Problem Demo Screen
So you see the result I want is that $test in the comment string of the 'Demo Screen' above should also be colored as gray!(See below.)
Can anyone help me solve this problem?
I'm Aware of highlight_string() function!
THANKS IN ADVANCE!
Reinventing the wheel?
highlight_string()
Also, this is why they have parsers, and regex (despite popular demand) should not be used as a parser.
I agree, that you should use existing, parsers. Every ide has a php parser, and many people have written more of them.
That said, I do think it is worth the mental exercise. So, you can replace:
$result = preg_replace("/(?<!\"|')[\$](?!\()(.*?)(?=(\W))/","<span style=\"color:green;\">$0</span>",$result);
with
//regular expression.:
//#([^(%%%%|\"|')]*)([\$](?!\()(.*?)(?=(\W)))#
//replacement text:
//$1<span style=\"color:green;\">$2</span>
$result = preg_replace("#([^(%%%%|\"|')]*)([\$](?!\()(.*?)(?=(\W)))#","$1<span style=\"color:green;\">$2</span>",$result);
Personally, I think your best bet is to use CSS selectors. Replace style=\"color:gray;\" with class="comment-text" and style=\"color:green;\" with class="variable-text" and this CSS should work for you:
.variable-text {
color: #00E;
}
.comment-text .comment-text.variable-text {
color: #DDD;
}
Insert don't use regex to parse irregular languages here
anyway, it looks like you've run into a prime example of why regular expressions are not suited for this kind of problem. You'd be better off looking into PHP's highlight_string functionality
Well, you don't seem to care that php already has a function like this.
But because of the structure of php code one cannot simply use a regex for this or walk into mordor (the latter being the easier).
You have to use a parser or you will fly over the cuckoo's nest soon.
I'm just about ready to cry. I have read the php.net manual page, tried a dozen Google searches with various phrases, and scanned hundreds of stackoverflow threads. I have seen this method in use, but it just doesn't work for me. It seems so basic. I have a couple related questions or problems I don't know how to find answers to.
The important part of the PHP file looks like this:
switch() {
…other cases…
default:
$tpl['title'] = "Newsletter Signup";
$tpl['description'] = "Newsletter description";
$tpl['page-content'] = file_get_contents('signup.html');
}
$tpl_src = addslashes(file_get_contents('index.tpl'));
eval("\$html = \"$tpl_src\";");
echo $html;
My index.tpl file includes lines like these:
<title>{$tpl['title']}</title>
<meta name="description" content="{$tpl['description']}" />
nav, etc…
<div id="main-content"> {$tpl['page-content']} </div>
I like how neat and clean the code is, without a whole bunch of extra <?=…?>'s.
First, when curly brackets {} appear in a string, what is that called? I might be able to look it up and learn how to use them if I knew.
Next, this just doesn't work at all. If I remove the single quotes from the variable keys, it's good, but php.net says you should never do that in case my name becomes a language constant at some point. Fair enough. But how do I fix this? I like using an array for the vars in case I want to build an evalTemplate subroutine and can just pass $tpl to it.
Lastly, $tpl['page-content'] doesn't print out. The variable is set okay; I can use echo $tpl['page-content'] to test, but it appears as a single blank line in the final HTML.
I'm sure there's just some aspect of the language I don't know yet. Any help is much appreciated!!
As Volker pointed out, addslashes seems to be an issue. Try addcslashes instead. Also, I'd strongly recommend making this a function, to simplify sanitisation / parsing:
function render ($file, $vars)
{
// .. extra sanitisation, validation, et al.
$_html = '';
$_raw_file = addcslashes (file_get_contents ($file), '"\\');
extract ($vars, EXTR_SKIP);
eval ('$_html = "'.$_raw_file.'"');
return $_html;
}
And called thus:
switch() {
// …other cases…
default:
$tpl['title'] = "Newsletter Signup";
$tpl['description'] = "Newsletter description";
$tpl['page-content'] = render ('signup.html');
}
echo render ('index.tpl', $tpl);
PS: The use of extract above means that your vars will simply be $title, not $tpl['title'], etc.
Usually you don't use the '' string delimiters in string variable expansion. I.e. "$tpl[content]" instead of "$tpl['content']".
As for as the braces, they delimit variable's when identifier characters may come straight before or after the name. For example:
$item = "Cup";
$text = "I smashed four $items"; // won't work
$text = "I smashed four {$item}s"; // will work.
// 2nd output: "I smashed four Cups"
addslashes() adds slashes before both single- and double-quotes within the string.
The code generated for your example would be
$html = "<title>{$tpl[\'title\']}</title>
<meta name=\"description\" content=\"{$tpl[\'description\']}\" />
nav, etc…¦
<div id=\"main-content\"> {$tpl[\'page-content\']} </div>";
And {$tpl[\'title\']} doesn't parse well.