在url字符串中编码html实体


Encoding html entities in url string

我正试图准备一个url,其中有特殊字符,在php文件中用作GET变量。我认为我需要html实体和urlencode,然后我需要在其他php文件解码。但是我在正确编码它们时遇到了一些问题。这是我的文件:

<?php $title = "This is a ‘simple’ test"; ?>
<?php $titleent = htmlentities($title); ?>
<?php $titleentencoded = urlencode($titleent); ?>
<?php $date = '21-12-2011'; ?>
    <p>Title: <?php echo $title; ?></p>
    <p>Title html entities: <?php echo $titleent; ?></p>
    <p>Title encoded: <?php echo $titleencoded; ?></p>
    <p><a href="index.php?title=<?php echo $titleencoded; ?>&date=<?php echo $date; ?>">Go!</a></p>

$titleencoded变量结果为空。我忽略了一些明显的东西,但我看不见。我做错了什么?


EDIT:建议后的新代码

好的,这是我想出来的:

<?php $title = "This is a ‘simple’ test"; ?>
<?php $titleentencoded = urlencode($title); ?>
<?php $htmlent = htmlentities($titleentencoded); ?> 
<?php $date = '21-12-2011'; ?>
<p>Title: <?php echo $title; ?></p>
<p>Title encoded: <?php echo $titleentencoded; ?></p>
<p>Title html entities: <?php echo $htmlent; ?></p>
<p><a href="index.php?title=<?php echo $htmlent; ?>&date=<?php echo $date; ?>">Go!</a></p>

这条路对吗?

你的变量是空的,因为你有一个打字错误。初始化$titleentencoded,但稍后使用$titleencoded:

<?php $titleentencoded = urlencode($titleent); ?>
// Should be
<?php $titleencoded = urlencode($titleent); ?>

查看@Quentin的逻辑错误答案

你做反了。

你把数据放到一个URL中,然后把URL放到一个HTML文档中。

你需要urlencode的数据,把它放在URL ,然后 html代码的URL,并把它放在文档