如何在我的例子中使用urlencode()?

时间:2013-02-22 06:21:25

标签: php forms get urlencode

我查看了php.net,并阅读了一些urlencode( )如何运作的示例,但不知怎的,我无法做到正确。有人可以帮我一把吗?

这将是很多例子,所以希望我的简短例子是有意义的。

我有一个名为2.php的页面,调用它来显示.txt中选择的1.php文件的一些内容。

我被告知要为3.php建立一个链接,该链接应该类似于/3?filename=a.txt 文件名为GET参数名称,并使用GET函数确保urlencoded参数值为urlencode( )

但我很困惑我应该如何以及在哪里放置urlencode()来使其发挥作用。

我会在这里粘贴我的2.php代码......我简化了代码...

<?php

$fileContents = file("./aaa/" . $_GET["course"] . ".txt");

echo "<table border=\"1\">";

foreach($fileContents as $row)
{
    echo "<tr>";
    $contents = preg_split("/,/", $row);

    foreach($contents as $eachline)
    {
        echo "<td>";
        if(!(preg_match("/@/", $eachline)))
        {
        echo trim(ucfirst($eachline));
        }
        else
        {
        echo trim(strtolower($eachline));
        }
        echo "</td>";
    }
    echo "</tr>";
}

    echo "</table>";

    echo "<a href='./1.php'>Choose another txt file</a><br/>";
    echo "or<br/>";
    echo "<a href='.3.php?'>Work with this txt file</a>";
?>

但是... 3.php选项必须附加一个查询字符串:在1中选择的文本文件的名称,因此,而不是./3.php,url应该是诸如./3之类的内容。 ?文件名= asdf.txt

使用“filename”作为GET参数名称。确保使用urlencode()函数对GET参数值进行urlencoded。

但我只是不确定如何让它发挥作用......

1 个答案:

答案 0 :(得分:4)

您可以将应该在函数中进行url编码的部分包装在字符串中:

$url = 'http://www.google.com?q=' . urlencode($search);

OR in html

http://www.google.com?q=<?php echo urlencode($search); ?>

其中.是2个输出的串联。