且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将变量传递到url中

更新时间:2023-11-19 21:33:40

您的问题在此行上:

$html = file_get_html('http://www.championselect.net/champ/{$var}');

单引号不能处理字符串替换.您可以在此处阅读 PHP如何处理字符串的方式.因此,将设置该URL的完整值(包括文字{$var}),而不是您所期望的值.您可以通过几种不同的方式解决此问题.例如使用允许字符串替换的双引号:

Single quotes don’t handle string substitution. You can read up on how PHP handles strings here. So the full value of that URL—including that literal {$var}—are being set instead of what you expect. You can tackle this issue a few different ways. Such as using double-quotes which allow string substitution:

$html = file_get_html("http://www.championselect.net/champ/{$var}");

或者只是将这些值串联起来:

Or just concatenating the values like this:

$html = file_get_html('http://www.championselect.net/champ/' . $var);

但是我更喜欢使用 sprintf ,它允许您将字符串用作这样的模板: /p>

But I prefer to use sprintf which allows you to use strings as templates like this:

$html = file_get_html(sprintf('http://www.championselect.net/champ/%s', $var));

除此之外,您的代码还有些草率&不一致,所以这里是我的清理工作.看起来似乎没什么大不了的,但是混乱的代码简直很难阅读,这使得调试变得很困难.更清晰的代码使您可以更快地发现缺陷.另外,混合HTML和PHP有点令人困惑.在像这样的简单情况下,大部分代码都是错误的.而且在这种情况下,在通过PHP解析所有内容的同时犯了错误:

Beyond that, your code is a bit sloppy & inconsistent, so here is my cleanup. It might not seem like a big deal, but chaotic code is simply hard to read which makes it hard to debug. Cleaner code allows you to spot flaws quicker. Also, mixing HTML & PHP is a bit confusing. In simple cases like this err on the side of what the bulk of the code is. And in this case, err on the side of just parsing it all through PHP:

<?php

// Echo the form.
echo '<form action="test.php" method="post">'
   . '<input type="text" value="' . $var . '" name="var" />'
   . '<input type="submit" value="Send" />'
   . '</form>'
   ;

// Set the $var variable.
$var = $_POST['var'];

// Include the library to use it.
include_once('simple_html_dom.php');

// Get div from site. ... Need to implement user selected queries.
$html = file_get_html(sprintf('http://www.championselect.net/champ/%s', $var));

// Put all of the <a> tags into an array named $result
$result = $html -> find('h2,img[class=CS_Champ_Image],.counterName' );

// Run through the array using a foreach loop and print each link out using echo
foreach($result as $element) {
  echo $element . " ";
}   

?>

编辑,我刚刚发现了其他地方.在您刚开始的代码或上面刚刚整理的版本中查看代码:

EDIT I just noticed something else. Look at the code right a the beginning of yours or the cleaned up version I just did above:

// Echo the form.
echo '<form action="test.php" method="post">'
   . '<input type="text" value="' . $var . '" name="var" />'
   . '<input type="submit" value="Send" />'
   . '</form>'
   ;

// Set the $var variable.
$var = $_POST['var'];

因此,在您的<form … >/</form>标记中,您将$var的值放入了'<input type="text" value="' . $var . '" name="var" />'中.但是,在呈现表单之后的$var之前,不会为它分配值吗?所以不应该这样删除它吗?

So in your <form … >/</form> tags you are placing the value of $var into '<input type="text" value="' . $var . '" name="var" />'. But that $var is not assigned a value until the after the form is rendered? So shouldn’t that just be removed to be like this?

// Echo the form.
echo '<form action="test.php" method="post">'
   . '<input type="text" value="" name="var" />'
   . '<input type="submit" value="Send" />'
   . '</form>'
   ;