Give colour to PHP string - php

Hello everyone I am trying one very simple thing but unfortunately for a reason that I don't get I can't have the desired result.I am trying to colourize a string everytime that is beign generated. I am running the example to an online interpreter and this is what I get back.
<?php
echo "<p style='color:red;'>Hello Wolrld</p>";
?>
Result :
<p style='color:red;'>Hello Wolrld</p>
The whole thing is treated as a string from the echo which seems ok but I don't know how to structure or write it in order to get back the word Hello World in red.
Thank you very much

Code works ok. Interpreter does not show PHP output properly, but shows HTML code encoded.
If you will try that code in your local server (XAMPP, or similar) it will work OK. It is a problem on interpreter side.

Related

Catch py_compile.PyCompileError, and convert to string

I'm trying to compile a python script and record any syntax errors
I have a script called test.py that looks like
#!/usr/bin/python
import py_compile
try:
py_compile.compile("answer.py")
except py_compile.PyCompileError as e:
print(e)
answer.py is just a self contained script that doesn't do anything except print something(for now)
i also have a php exec command in a php file that runs test.py, problem is php exec doesn't get returned anything by
print(e)
I'm guessing because e here in PyCompileError isn't a string or something weird with the format, the result simply comes back empty "". When I played around with it it seems to be an object, not sure how to convert that to a string.
I tried putting it into a string but it's not working, if I change e into a string like print("You have errors") I get the correct output, so it's not my php but I want the syntax errors on the lines. Not even sure if I should be using compile() for this
I figured it out, basically I needed doraise=True and then get print(e.exc_value)
I did initially have doraise=True before people think I was lazy and not reading the documentation, but I didn't realize exc_value was the variable that had the message(I was trying to get e.msg) so I turned it off since it said by default it prints to stderr with it off, and I tried to work from there.
Please ignore

modulus behaving differently on different servers? (with / without spaces)

The following code
<?php
echo ((12+1)%12)."<br/>";
echo ((12+1) % 12)."<br/>";
?>
leads to an unexpected result (13,1) instead of (1,1) on phpfiddle.org but it runs as expected on my server.
http://phpfiddle.org/lite/code/qdb-s4t
Is this an error in their sandbox or does it have to do with different php versions? How is the case without spaces interpreted?
I was just looking at some code for quite a long time and couldn't understand what was the difference.
i know i could use fmod or other sandboxes like http://ideone.com/.
PHPFiddle is just a website that is attempting to provide an easy way to execute PHP code samples from the browser. This isn't going to give you native behavior, simply because the code is going to be processed by JavaScript first using whatever logic the people at PHPFiddle seem fit. This leads to the possibility of bugs that have nothing to do with PHP and that is what is going on here. If you turn those same lines of codes into full strings, you will see the output still isn't even correct.

Weird sprintf behavior in PHP

I was trying to do sprintf("<%s>", "Sat");, but nothing comes out. When you remove the less than symbol, it will start working again. Anyone experience this behavior and whether it expected? as i think it is a bug.
You can even get the same result here with printf.....
http://writecodeonline.com/php/
Your browser is probably rendering it as a tag. View source to confirm.
http://codepad.org/g5FXZAwa
<?php
printf("<%s>", "Sat");
Prints <Sat>
Edit for Yogesh.
<?php
echo sprintf("<%s>", "Sat");
Prints <Sat>
I believe that this happens because <Sat> is interpreted by your browser as a tag.

PHP echo adds a new line

Could somebody explain me why the PHP tags are giving me a linebreak?
And also, how can you delete this or stop this from happening, as it messes up my site.
An example I'm using on my site:
<?php include('assets/common/theme_header.php'); ?>
EDIT:
This doesn't seem to happen when I'm using:
<?php ?>
It does however seem to happen only when I'm using echo, which I also use on my include.
Example:
<?php echo "hello"; ?>
This still gives me a "linebreak", and it shows like this in Chrome development kit:
I had a similar situation where a php file on the server always echoed a space, then newline and then the actual echo. Like " \n[someVariable]". I got rid of it by making sure the php file had no empty lines at the beginning or end of the file.
So no empty line before the <?php or after the end ?>
Perhaps your problem is related.

how to eval() a segment of a string

I have a string that has HTML & PHP in it, when I pull the string from the database, it is echo'd to screen, but the PHP code doesn't display. The string looks like this:
$string = 'Hello <?php echo 'World';?>';
echo $string;
Output
Hello
Source Code
Hello <?php echo 'World';?>
When I look in the source code, I can see the php line there. So what I need to do is eval() just the php segment that is in the string.
One thing to consider is that the PHP could be located anywhere in the string at any given time.
* Just to clarify, my PHP config is correct, this is a case of some PHP being dumped from the database and not rendering, because I am echo'ing a variable with the PHP code in it, it fails to run. *
Thanks again for any help I may receive.
$str = "Hello
<?php echo 'World';?>";
$matches = array();
preg_match('/<\?php (.+) \?>/x', $str, $matches);
eval($matches[1]);
This will work, but like others have and will suggest, this is a terrible idea. Your application architecture should never revolve around storing code in the database.
Most simply, if you have pages that always need to display strings, store those strings in the database, not code to produce them. Real world data is more complicated than this, but must always be properly modelled in the database.
Edit: Would need adapting with preg_replace_callback to remove the source/interpolate correctly.
You shouldn't eval the php code, just run it. It's need to be php interpreter installed, and apache+php properly configured. Then this .php file should output Hello World.
Answer to the edit:
Use preg_replace_callback to get the php part, eval it, replace the input to the output, then echo it.
But. If you should eval things come from database, i'm almost sure, it's a design error.
eval() should work fine, as long as the code is proper PHP and ends with a semicolon. How about you strip off the php tag first, then eval it.
The following example was tested and works:
<?php
$db_result = "<?php echo 'World';?>";
$stripped_code = str_replace('?>', '', str_replace('<?php', '', $db_result));
eval($stripped_code);
?>
Just make sure that whatever you retrieve from the db has been properly sanitized first, since you're essentially allowing anyone who can get content into the db, to execute code.

Categories