简体   繁体   English

PHP数组到链接内的变量

[英]PHP array to variable inside a link

I'm using the code from this website: http://blog.chapagain.com.np/php-how-to-get-stock-quote-data-from-yahoo-finance-complete-code-and-tutorial/ 我正在使用来自以下网站的代码: http : //blog.chapagain.com.np/php-how-to-get-stock-quote-data-from-yahoo-finance-complete-code-and-tutorial/

$stock = "GOOG";
$objYahooStock->addFormat("sl1"); 
$objYahooStock->addStock($stock);

foreach( $objYahooStock->getQuotes() as $code => $stock) {
    $stock = $stock[1];
}

However, I can't use the variable $stock inside a link. 但是,我不能在链接内使用变量$stock If I try to display it on the page ( echo $stock; ) it's working. 如果我尝试将其显示在页面上( echo $stock; ),它正在工作。 I'm able to see the stock price, but I'm not able to use the variable inside a link, see below. 我可以查看股票价格,但无法在链接内使用变量,请参见下文。

http://example.com/example?text=' . $stock . '%20USD

I've also tried saving the stock price to a variable without the foreach loop. 我还尝试过将股票价格保存到没有foreach循环的变量中。

$stock = "GOOG";
$price = $objYahooStock->getQuotes();
$stock = $price[$stock][1]; 

Same result here. 同样的结果在这里。 I'm able to display the price but I can't use it inside a link. 我可以显示价格,但不能在链接中使用它。

Does anyone see the issue? 有人看到这个问题吗? Thanks in advance. 提前致谢。

Without seeing your full code, it's difficult to see what you are doing, but http://example.com/example?text=' . $stock . '%20USD 如果没有看到完整的代码,很难看到您在做什么,但是http://example.com/example?text=' . $stock . '%20USD http://example.com/example?text=' . $stock . '%20USD http://example.com/example?text=' . $stock . '%20USD looks as if this is HTML and not PHP? http://example.com/example?text=' . $stock . '%20USD看起来好像是HTML而不是PHP?

Depending on your approach ... 根据您的方法...

PHP: echo 'http://example.com/example?text=' . $stock . '%20USD'; PHP: echo 'http://example.com/example?text=' . $stock . '%20USD'; echo 'http://example.com/example?text=' . $stock . '%20USD';

or; 要么;

HTML: http://example.com/example?text=<?=$stock?>%20USD HTML: http://example.com/example?text=<?=$stock?>%20USDhttp://example.com/example?text=<?=$stock?>%20USD

EDIT: I see you are using JavaScript from your comment, so use the HTML example above. 编辑:我看到您从您的注释中使用JavaScript,所以使用上面的HTML示例。

Basically you need to analyse output. 基本上,您需要分析输出。 Try var_dump and see the result. 尝试var_dump并查看结果。

In your case $stock have some character in string. 在您的情况下, $stock字符串中有一些字符。 try below solution for snl1d1t1cv format: 请尝试以下针对snl1d1t1cv格式的解决方案:

$objYahooStock = new YahooStock;
$objYahooStock->addFormat("snl1d1t1cv");
$objYahooStock->addStock("GOOG");

$price = $objYahooStock->getQuotes();
$stock = "GOOG";
$stock = $price[$stock][1];

var_dump($price);
echo '<script>window.open("http://example.com/example?text=' . str_replace('"','',$stock) . '%20USD");</script>'

This solution opening a new window at my machine 该解决方案在我的机器上打开了一个新窗口

Edit: for the format sl1 below solution will work: 编辑:对于下面的解决方案格式sl1将起作用:

$objYahooStock = new YahooStock;

$objYahooStock->addFormat("sl1");

$objYahooStock->addStock("GOOG");


$price = $objYahooStock->getQuotes();
$stock = "GOOG";
echo $stock = $price[$stock][1];

var_dump($price);
echo '<script>window.open("http://example.com/example?text=' . str_replace('\n','',trim($stock)) . '%20USD");</script>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM