【问题标题】:How can I replace %20 with - from url using php?如何使用 php 将 %20 替换为 - from url?
【发布时间】:2017-03-31 14:52:43
【问题描述】:

代码:

 <?php
    $query = "select * from latest_news limit 0,10";
    $fet = mysqli_query($link,$query);
    while ($fetch = mysqli_fetch_array($fet)) 
    {
 ?>
 <p id="news-h3"><?php echo $fetch['news_title']; ?>...<a href="news-details.php?news_tags=<?php echo $fetch['news_tags']; ?>" style="color:red;">[Read More]</a></p>
 <?php
     }
 ?>

当我点击链接时,即

<a href="news-details.php?news_tags=<?php echo $fetch['news_tags']; ?>" style="color:red;">[Read More]</a>

它显示

best%20engineering%20college%20in%20India

在这里,我想将 %20 替换为 url 中的 (-) 并获得类似的结果

best-engineering-college-in-India

我该如何解决这个问题,请帮忙。

谢谢

【问题讨论】:

    标签: php jquery


    【解决方案1】:

    1.你可以使用这个php内置函数。你只是想用另一个替换一个字符串。

    $new_url = str_replace('%20', '-', $url);
    

    更多信息

    http://fr2.php.net/str_replace

    2.使用urlencode进行编码。解码会自动进行, 如果您想删除总共 20%,请使用此代码。

     $url = "one%20%26%20two";
     $a = urldecode($url); // -> "one & two"
    

    加载时假设您的页面,然后像这样创建您的网址

     $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $base_url = $protocol . $_SERVER["SERVER_NAME"];
    

    那就用吧

    urldecode($base_url)
    

    如果你想在 url 中使用它,那么

     echo '<a href="?mycgi?foo=', urlencode($userinput), '">';
    

    更多信息

    http://www.php.net/manual/en/function.urldecode.php

    【讨论】:

    【解决方案2】:

    你应该在这里使用str_replace()

    echo str_replace('%20', '-', $input); // best-engineering-college-in-India
    

    【讨论】:

      【解决方案3】:

      基本上你想对 URL 进行解码。您可以使用

      对其进行解码
      echo utf8_decode(urldecode($input));
      

      【讨论】:

      • 从数据库中获取数据时如何使用 urldecode
      猜你喜欢
      • 2011-10-08
      • 1970-01-01
      • 2012-03-03
      • 1970-01-01
      • 2015-05-09
      • 2012-01-08
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      相关资源
      最近更新 更多