I have a string stored in a variable in my php code, something like this:
<?php
$string = "
<?php $var = 'John'; ?>
<p>My name is <?php echo $var; ?></p>
";
?>
Then, when using the $string variable somewhere else, I want the code inside to be run properly as it should. The PHP code should run properly, also the HTML code should run properly. So, when echoing the $string, I will get My name is John wrapped in a <p> tag.
You don't want to do this. If you still insist, you should check eval aka evil function. First thing to know, you must not pass opening <?php tag in string, second you need to use single quotes for php script You want to evaluate (variables in single quotes are not evaluated), so your script should look something like:
<?php
$string = '
<?php $var = "John"; ?>
<p>My name is <?php echo $var; ?></p>
';
// Replace first opening <?php in string
$string = preg_replace('/<\?php/', '', $string, 1);
eval($string);
However, this is considered very high security risk.
So if I'm right, you have the $var = 'John'; stored in a different place.
Like this:
<?php $var = 'John'; ?>
And then on a different place, you want to create a variable named $String ?
I assume that you mean this, so I would suggest using the following:
<?php
$String = "<p>My name is ".$var." </p>";
echo $String;
?>
You should define $var outside the $string variable, like this
<?php
$var="John";
$string = "<p>My name is $var</p>";
?>
Or you can use . to concatenate the string
<?php
$var="John";
$string = "<p>My name is ".$var."</p>";
?>
Both codes will return the same string, so now when doing
<?php
echo $string;
?>
You will get <p>My name is John</p>
Related
<?php echo $row["html"]; ?>
Inside of the $row["html"] there's:
<?php $Site->Nav($owner); ?>
but when I echo it, it only echoes:
Nav($owner); ?>
How may I print the full and make it usable, which means that it will print the function Nav?
I've tried to replace <?php with [[// i the database, and just before echoing it, I change back with replace. But without success
I think you need to use eval function of php. See the example below.
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
Might be it can help.
Use eval function. It might solve your problem like this:
<?php echo eval($row["html"]); ?>
Keep the code as is in DB as if you are writing it in PHP file but without PHP opening and closing tags i.e. <?php and ?>. I haven't checked this (as i am not sure what $Site->Nav($owner); will do) but hope it would work in this case.
If I understand correctly you are wanting to output the results of $Site->Nav($owner);
I have no idea what this is expected to output, but assuming it is a string of some kind that you wish to display (hence echo) - an example of achieving this would be calling your code and have that method return the value, so you can echo it out. Ie:
function Nav($owner){
// Do your stuff
return 'Your Desired Output';
}
Then on your page you would have
<?php echo $Site->Nav($owner); ?>
Which would echo "Your Desired Output".
database has php value saved like that as string
<?php $s = 'my name is'; ?>
and what am trying to do is calling the value as php code and want when type echo $s; to print my name is but now it's given my empty value see the code below to get what i mean
<?php
$b = '<?php $s = 'my name is';?>';
echo $b; //empty value
?>
Sounds like you're talking about eval() - but I'd be wary of using it. If you do, be extremely careful.
"If eval() is the answer, you're almost certainly asking the wrong question." -Rasmus Lerdorf
You'd probably need to strip the <?php and ?> tags, and watch for double quotes surrounding variables you don't want to replace:
$s=0;
eval('$s = "my name is";');
echo $s;
PHP is not recursively embeddable:
$b = '<?php $s = 'my name is';?>';
That's not PHP code in there. it's some text with the letters <, ?, p, etc...
And you ARE getting output. But you're viewing it in a browser, so the <?php ... ?> gets rendered as an unknown/illegal html tag and simply not displayed. If you'd bothered doing even the most basic of debugging, e.g. "view source", you'd have seen your PHP "code" there.
I am writing an application that will look at a single record, obtain values from about 12 flags (0 or 1), look up those flags against a status table (in MySQL) and return a variable called $status_message which is in that table.
In this table I need to have hyperlinks (working fine) but also echo some variables, i.e.
You have no bids for {{$row->_item_name}}
or
View this item now by clicking here
Now I need item name and the other example to be translated into <?php echo $row->_item_name; ?>
I have tried a preg_replace with the following:
<?php
$find = array('/{{/', '/}}/');
$replace = array('<?php echo ', ' ?>');
echo preg_replace($find, $replace, $status_message);
?>
but this is not working.
Can anyone advise how I can get the desired result and 'echo' the variable in the MySQL field?
Had a brainwave. Much simpler,
instead of $row->_item_name I just put {{itemname}} in the string. I then use the following code:
<?php
$message_buyer = str_replace('{{itemname}}', $row->_item_name , $message_buyer);
echo $message_buyer;
?>
so no need to have <?php calls in the string at all.
I'm trying to make a dynamic webpage. I have the title for each page in its own file and I was trying to use file_get_contents() to get the title, but I'm not sure how to use a variable in the path. This is what I've tried.
<?php
$movie= $_GET["film"];
$title= file_get_contents('/movies/moviefiles/.$movie./info.txt');
?>
<h1><?= ($title) ?> </h1>
You aren't concatenating the strings properly.
Try:
$title= file_get_contents('/movies/moviefiles/'.$movie.'/info.txt');
The same can be done with double-quotes, too:
$title= file_get_contents("/movies/moviefiles/$movie/info.txt");
The difference is that variables aren't interpolated within single quotes. If they're in double-quotes, the actual value of the variable will be used.
And read more about string concatenation here: http://php.net/manual/en/language.operators.string.php
You need to use double quotes " when you want to use variables inside a string or concatenate using the ..
$foo = "world";
print "hello $foo";
print 'hello '.$foo;
Your code should be like this:
<?php
$movie= $_GET["film"];
$title= file_get_contents('/movies/moviefiles/'.$movie.'/info.txt');
?>
<h1><? echo $title; ?> </h1>
You can't work with variable with simple quote.
if you want to use variable, use double quote
$title= file_get_contents("/movies/moviefiles/$movie/info.txt");
More information on these quotes : http://www.php.net/manual/fr/language.types.string.php
$title= file_get_contents("/movies/moviefiles/$movie/info.txt");
I am trying to extract php code from a long file. I wish to throw away the code not in a PHP tags. Example
<html>hello world, its a wonderful day</html>
<?php echo $user_name; ?> Some more text or HTML <?php echo $datetime; ?>
I just echoed the user_name and datetime variables.
I want to return an array with:
array(
[1] => "<?php echo $user_name; ?>"
[2] => "<?php echo $datetime; ?>"
)
I think I can do this with regex but im not an expert. Any help? Im writing this in PHP. :)
You will have to view the source code in order to see the results, but this is what I came up with:
$string = '<html>hello world, its a wonderful day</html>
<?php echo $user_name; ?> Some more text or HTML <?php echo $datetime; ?>
I just echoed the user_name and datetime variables.';
preg_match_all("/<\?php(.*?)\?>/",$string,$matches);
print_r($matches[0]); // for php tags
print_r($matches[1]); // for no php tags
Update: As mentioned by Revent, you could have <?= for shorthand echo statments. It would be possible to change your preg_match_all to include this:
$string = '<html>hello world, its a wonderful day</html>
<?php echo $user_name; ?> Some more text or HTML <?= $datetime; ?>
I just echoed the user_name and datetime variables.';
preg_match_all("/<\?(php|=)(.*?)\?>/",$string,$matches);
print_r($matches[0]); // for php tags
print_r($matches[1]); // for no php tags
Another alternative is to check for <?(space) for the shorthand php statement. You can include a space (\s) to check for this:
preg_match_all("/<\?+(php|=|\s)(.*?)\?>/",$string,$matches);
I guess it just depends on how "strict", you want to be.
Update2: MikeM does make a good point, about being aware of line breaks. You may run into an instance where your tags run over into the next line:
<?php
echo $user_name;
?>
This can easily be solved by using the s modifier to skip linbreaks:
preg_match_all("/<\?+(php|=|\s)(.*?)\?>/s",$string,$matches);