在 GET 中用 %20 替换连字符

replace hyphen with %20 in GET

我重写了 url,因此 %20(space) 的每个实例都用连字符 (-) 代替。现在 %20 不影响 url 的检索,例如它读取 html%20and%20csshtml and css,但用连字符替换它认为 html and csshtml-and-css 因此我没有正确取回东西。

我的问题是我想在使用 GET 方法时用 space 代码替换连字符:

          $search_query2 = $_GET['crs_category'];

因此,我必须过滤 $_GET['crs_category']; 以将 "-" 替换为 space

您可以使用str_replace()

$search_query2 = str_replace("%20", "-", $_GET['crs_category']);

我的测试:

$crs_category = 'html%20and%20css';
$search_query2 = str_replace("%20", "-", $crs_category);
echo $search_query2;

产量:

html-and-css

我一遍又一遍地阅读你的问题,我得出的唯一合乎逻辑的结论是,你已经用连字符替换了原来的 %20。现在,您只想反转这个过程...?

如果是这样,一个简单的 str_replace 就可以解决问题。

$string = 'html-and-css';
$string = str_replace('-', ' ', $string);

echo $string;

输出: html%20and%20css

只需将我原来的 $string 替换为您的 $_GET array

$string = $_GET['crs_category'];