如何在PHP中使用urlencode

Dyl*_*SUN 0 php urlencode

我正在尝试使用urlencode转换字符串: <a href="<?php print 'search.php?query='.quote_replace(addmarks($search_results['did_you_mean'])).'&search=1'?>">

实际上,我想实现一个搜索引擎.

|-www
 |- index.php
 |- search directory
  |- search.php
  |- header.html
  |- search_form.html
  |- search_result.html
  |- footer.html
Run Code Online (Sandbox Code Playgroud)

search.php includes header.html,search_form.html,search_result.html etc.

我使用以下方法访问search.php: localhost/index.php/?page=search

search_form.html包含要搜索的按钮.它使用:调用search.php <form action="index.php/?page=search" method="get">.我不确定它是否正确.

提交搜索请求后,search.php调用search_result.html显示结果.search_result.html中的代码: <a href="<?php print 'search.php?query='.quote_replace(addmarks($search_results['did_you_mean'])).'&search=1'?>"><?php print $search_results['did_you_mean_b']; ?>

它似乎应该工作,但在我点击搜索按钮后,结果网址是index.php/?query=&search=1.而且我认为应该如此index.php/?page=search/?query=&search=1.

所以,我尝试使用urlencode来解决它.我不知道这个想法是否正确.

Ste*_*rig 10

$url = 'search.php?' . http_build_query(array(
    'query'  => $search_results['did_you_mean'],
    'search' => 1
));
Run Code Online (Sandbox Code Playgroud)

这是最简单的方法 - 请看http_build_query().

我不知道你的功能quote_replace()addmarks()做,但是当你跑urlencode("search.php?query=")这也将编码?=并会导致search.php%3Fquery%3D(同为urlencode("&search=1")编码的&=,并将导致%26search%3D1),其总将使URL无法使用.