PHP - Variable Questions - php

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?)

Related

Run PHP code stored in a database

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.

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 echo $this->item->link in a href not working - showing JObject->link

quick question as I'm pulling my hear out!
In a PHP file I have different items pulled through such as contact details etc. I have pulled through $this->item->link which shows a URL which is perfect. I just want to make the url clickable, so I had the following code:
echo "<a href='$this->item->contactlink' target='new'>";
echo $this->item->contactlink;
echo '</a>';
This link doesn't work, stays within the site with a 404 and the URL looks like: http://domain.co.uk/JObject->contactlink
What can I do or have I missed to get the link clickable?
Kind regards
Liam
You're hitting a PHP parser glitch: It's not "greedy". Using a nested object reference, or multidimensional array, in a "-quoted string only parses the FIRST "dimension" of the object or array as part of the object/array:
php > class z { function __toString() { return get_class($this); } }
php > $x = new z();
php > $x->foo = new z();
php > $x->foo->bar = new z();
php > echo "$x // $x->foo // $x->foo->bar";
z // z // z->bar
Note the z->bar on the final part of the echo. The PHP parser saw $x->foo and STOPPED parsing there, leaving ->bar as plain text, and not part of the object.
You need to use the {}-extended strings syntax:
php > echo "$x // $x->foo // {$x->foo->bar}";
z // z // z
Note how ->bar didn't get output.
And ditto for a arrays:
$foo['bar']['baz'] = 'qux';
echo "$foo[bar][baz] {$foo['bar']['baz']}";
PHP Notice: Array to string conversion in php shell code on line 1
Array[baz] qux
Note how $foo['bar'] became the word Array in the first portion, while qux was properly output in the second.
Try:
<a href="<?php echo $this->item->contactlink; ?>" target='_blank'>
<?php echo $this->item->contactlink; ?>
</a>
Or shorter version:
<a href="<?= $this->item->contactlink; ?>" target='_blank'>
<?= $this->item->contactlink; ?>
</a>
Target='new' may be part of the issue - I believe target=_blank is standard. Not 100% on the issue, without seeing how you are building '$this'. Also pulling the php elements away from the html helps clear the link reference. Maybe there is an extra quotation for the link?

php html page - take the URL, do a search replace on it, then output the result into the html

Hi I need a simple way with inline PHP to search/replace the URL string, then output the result into the HTML
Our checkout cart redirects people to our post-purchase page, and something like the following ends up in the URL bar:
http://example.com/postpurchasepage.php?amount=$9.99&firstname=Billy&lastname=Bob
More often, based on the browser, they end up with this in their URL bar:
http://example.com?amount=%249.99&firstname=Billy&lastname=Bob
(converts the $ into %24 )
Yes, the cart sends the $ symbol along inside the parameter value. No I don't want it, nor the %24. So, the challenge is to remove both the $ symbol or the %24 (whichever happens),
Right now I am trying to pull the amount value into the page using <?php echo #$_GET['amount']; ?>
For example:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
However that outputs:
<p>Hey Billy !</p>
<p>Thanks for donating %249.99</p>
I want it to output:
<p>Hey Billy !</p>
<p>Thanks for donating 9.99</p>
(or whatever the amount value is that was passed from the cart, without the $ symbol or the %24)
Like I said, basically I am looking for a way to search the URL bar for whatever I define (multiple 'or' type searching), replace with whatever I define, and output the result into the html
Any help appreciated!
You might be looking for urldecode()
echo urldecode("%24"); // $
Then you can strip off the leading $ (if any):
$string = (strpos($string)===0) ? substr($string, 1) : $string;
Manual: http://php.net/manual/en/function.urlencode.php
There are two ways (or more - depending on your creativity & scenario) to get this done;
If you have access to the value of amount before the redirecting occurs, you can strip off the "$" sign and just send in the numeric value and use it there.
Otherwise, you can get the value on the page, and then as willoller mentioned, urldecode it and you can then strip the any leading character that is not numeric. (Not sure if performance would be an issue but that's what worked for me several times before)
Here's a sample for your code:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
<?php
$Get_Amount = urldecode($_GET['amount']);
// WHILE LOOP = TO VALIDATE IF VALUE IS NUMERIC
while (is_numeric($Get_Amount)==false) {
if (is_numeric(substr($Get_Amount, 0,1))){
// DO NOTHING = First Character Numeric
// Might be an issue if you have characters in between the number
// THIS WILL CAUSE RE-LOOP PROBLEM (Dependent on your scenario)
}else{
// TRIM OFF THE FIRST CHARACTER
$Get_Amount = substr($Get_Amount, 1);
}
}
?>
<p>Thanks for donating <?php echo $Get_Amount; ?></p>
Hope this helps! Good Luck!
OR -----------------------
You can use the preg_replace function like this:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
<?php
$String_Get_Amount = urldecode($_GET['amount']);
// Here you can add the characters that you wanna delete
// You should not delete anything that is not numeric cause then it would even replace the decimal point
$String_Search_For_Pattern = '/[$]/';
// Replace it with nothing
$String_To_Replace = '';
$String_Get_Amount = preg_replace($String_Search_For_Pattern, $String_To_Replace, $String_Get_Amount);
?>
<p>Thanks for donating <?php echo $String_Get_Amount; ?></p>

How should I escape?

its a simple logger function. I want to load an array to a DIV content:
document.getElementById('layout').innerHTML = '<?php foreach ($logs as $item) { echo str_replace(array('"',"'"), array ('"','''), $item).'<hr />'; } ?>';
because $logs can contain HTML elements, but not quotes, since they would ruin the echoing. It should be OK, but Firefox say "malformed Unicode character sequence" and it doesnt displayed. Now what?
use html entities to translate your html code into safe sequencies
I might be wrong, but it looks like you're trying to enter php code into your page using javascript. Javascript is client side, so if anything, you will get the full code, not the parsed code.
If you want the php to be run beforehand, so the actual code in your page will be a javascript that has the allready-parsed contents in it, you neet to make a string that contains the finished content AND add "'" for javascript to understand it.
This might look like this (haven't checked the foreach for you)
<?//this part is parsed before sending it to the client
$conts = "'";
foreach ($logs as $item) {
$conts .= str_replace(array('"',"'"), array ('"','''), $item);
$conts .= "<hr/>";
}
$conts .= "'";
?>
document.getElementById('layout').innerHTML = <? echo $conts ?>;

Categories