Run PHP code stored in a database - php

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.

Related

preg_replace of php code

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.

PHP - Variable Questions

Not sure of a better title for this question but basically heres my code:
<?php
$v = file_get_contents('http://examplecom/index.php?=');
$popularnames = array('Gravity Falls','The Amazing World of Gumball','South Park','American Dad');
?>
Basically what im trying to do is reference the $popularnames array in different areas so like in my href for example this is what i need:
<?= $popularnames[0]; ?>
Basically what it should be doing is adding popularnames[0] (Gravity Falls) url encoded to the file_get_contents url of $v but for some reason all it does is output Gravity Falls
But my result should have gotten me http://example.com/index.php?=Gravity+Falls and then have do file_put_contents to it and its result should then have been Hello World! or whatever the page was displaying.
I cant just add $popularname[0] to the $v as im using the $v in multiple href's so doing that it will make all the hrefs one url but I want them different like:
<?= $popularnames[0]; ?>
<?= $popularnames[1]; ?>
<?= $popularnames[2]; ?>
Using this works fine doe so theres no issue with what the variable contain:
<?php $v = file_get_contents('http://example.com/index.php?q='.urlencode($popularnames[0])); echo $v; ?>
Any ideas?
From an answer below how can I make it more compact so it would preferably make me able to just do <?= $v.$v2.$popularnames[0]; ?>:
<?php
$v = 'http://example.com/index.php?q=';
$v2 = file_get_contents($v.urlencode($popularnames[0])); echo $v2;
?>
Do note that the echoed $v2 will be echoed inside a src=" "
But my result should have gotten me http://example.com/index.php?=Gravity+Falls
Then just store the URL fragment in $v:
$v = 'http://examplecom/index.php?=';
Currently you're getting the file contents of that URL fragment and storing that in $v:
$v = file_get_contents('http://examplecom/index.php?=');
If the contents of that incomplete URL are empty, then naturally $v would be empty. And concatenating an empty string with another string would result in just that other string.
(Side note: I'm not sure what you plan to do at index.php since the query string value you're sending doesn't have a key associated with it. Maybe you forgot to include the key in the URL fragment?)

How to pass a PHP variable using the URL

I want to pass some PHP variables using the URL.
I tried the following code:
link.php
<html>
<body>
<?php
$a='Link1';
$b='Link2';
echo 'Link 1';
echo '<br/>';
echo 'Link 2';
?></body></html>`</pre></code>
pass.php
<pre><code>`<html>
<body>
<?php
if ($_GET['link']==$a)
{
echo "Link 1 Clicked";
} else {
echo "Link 2 Clicked";
}
?></body></html>
Upon clicking the links Link1 and Link2, I get "Link 2 clicked". Why?
In your link.php your echo statement must be like this:
echo '<a href="pass.php?link=' . $a . '>Link 1</a>';
echo 'Link 2';
Then in your pass.php you cannot use $a because it was not initialized with your intended string value.
You can directly compare it to a string like this:
if($_GET['link'] == 'Link1')
Another way is to initialize the variable first to the same thing you did with link.php. And, a much better way is to include the $a and $b variables in a single PHP file, then include that in all pages where you are going to use those variables as Tim Cooper mention on his post. You can also include this in a session.
You're passing link=$a and link=$b in the hrefs for A and B, respectively. They are treated as strings, not variables. The following should fix that for you:
echo 'Link 1';
// and
echo 'Link 2';
The value of $a also isn't included on pass.php. I would suggest making a common variable file and include it on all necessary pages.
I found this solution in "Super useful bits of PHP, Form and JavaScript code" at Skytopia.
Inside "page1.php" or "page1.html":
// Send the variables myNumber=1 and myFruit="orange" to the new PHP page...
Send variables via URL!
//or as I needed it.
<a href='page2c.php?myNumber={$row[0]}&myFruit={$row[1]}'>Send variables</a>
Inside "page2c.php":
<?php
// Retrieve the URL variables (using PHP).
$num = $_GET['myNumber'];
$fruit = $_GET['myFruit'];
echo "Number: ".$num." Fruit: ".$fruit;
?>
All the above answers are correct, but I noticed something very important. Leaving a space between the variable and the equal sign might result in a problem. For example, (?variablename =value)
Use this easy method
$a='Link1';
$b='Link2';
echo "Link 1";
echo '<br/>';
echo "Link 2";
You can do this like this:
<a href="./gemsPack.php?uid=<?php echo $id; ?>&amount=1.99">
just put
$a='Link1';
$b='Link2';
in your pass.php and you will get your answer and do a double quotation in your link.php:
echo 'Link 1';

Changing Text in PHP

I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...
I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.
So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:
<?php
$something = "this will replace existing text in the something paragraph...";
?>
Can somebody please point me in the right direction? As the above did not work.
Thank you :)
UPDATE
I was able to get it working using the following sample:
Place this code above the <html> tag:
<?php
$existing = "default message here";
$something = "message displayed if form filled out.";
$ne = $_REQUEST["name"];
if ($ne == null) {
$output = $existing;
} else {
$output = $something;
}
?>
And place the following where ever your message is to be displayed:
<?php echo $output ?>
As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:
<?php
$existing = "existing text";
$something = "this will replace existing text in the something paragraph...";
if (empty($_GET['button'])) {
$output = $existing;
} else {
$output = $something;
}
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>
but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?
If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.
If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.
If you are looking for string manipulation and conversion you can simply use the str_replace function in php.
Please check this: str_replace()
If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.

php echo doesnt print newlines

I started doing a new project in PHP / MySql . The Aim of this project is to manage articles for a magazine that i am the editor of.
So the content of the articles, i decided i would store with the "TEXT" column type of MySql
Now when i retrieve this column and print it with echo, the newlines are not there. Its all on the same line.
$resset = mysql_query("select txt from articles where id = 1");
$row = mysql_fetch_assoc($resset);
$txt = $row['txt'];
echo $txt; //doesnt print it as it is in the database as it was typed (multiline)
Find below, the text as it looks in the database and as it looks when it is echoed
in the databse, it is with new lines http://img413.imageshack.us/img413/4195/localhostlocalhostzxcvb.jpg
Text as it looks when echod http://img718.imageshack.us/img718/1700/sdashboardmozillafirefo.jpg
But within the database, its stored with newlines.
Has anybody else encountered this problem?
Please help me as my project depends on this :|
Whitespace in HTML is folded into a single space. Use nl2br() if you want to maintain newlines in HTML.
Have you tried
echo $txt."<br/>";
alternatively, you can put your output between <pre> tags:
echo "<pre>";
$resset = mysql_query("select txt from articles where id = 1");
$row = mysql_fetch_assoc($resset);
$txt = $row['txt'];
echo $txt; //doesnt print it as it is in the database as it was typed (multiline)
echo "</pre>";
btw,
echo $txt."<br/>";
may not work since it will just append newline at the end but not within $txt string
I know this isn't part of your question, but if you additionally want new lines in your HTML code but not your presentation, use double quotes with \n. This can help keep the HTML really tidy.
echo "\n";

Categories