代码:
<?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
在这里,我想用 url 中的 (-) 替换 %20 并得到如下结果
best-engineering-college-in-India
如何解决这个问题,请帮忙。
谢谢
最佳答案
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), '">';
了解更多信息
关于php - 如何使用 php 将 %20 替换为 url 中的 - ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43133230/