'how to remove %20 in the url in php
Current Code:
<?php
include('conn.php');
$sql = mysql_query("select min(news_id) 'min' from news");
$res = mysql_fetch_array($sql);
$qry=mysql_query("select * from news order by date(post_date) desc,priority desc ");
while($row1=mysql_fetch_array($qry)) {
<p class='news_title'><a href='newsdetail.php?id=".urldecode($row1['news_heading'])."'>{$row1['news_heading']}</a></p>
}
?>
As I am passing the heading to the next page ...
The url in the next page is displaying like this
newsdetail.php?id=In%20front%20%20of%20the%20houses
I need to display like this:
newsdetail.php?id=In front of the houses
Solution 1:[1]
You can use urldecode
to convert the URL string.
http://www.php.net/manual/en/function.urldecode.php
echo urldecode('newsdetail.php?id=In%20front%20%20of%20the%20houses');
will produce:
newsdetail.php?id=In front of the houses
Solution 2:[2]
You can't have spaces or special characters in the URL. If you try to put them in, then the browser will put %20 for spaces.
for clean url please replace the space or special charcters with hypen(-)
function cleanURL($textURL) {
$URL = strtolower(preg_replace( array('/[^a-z0-9\- ]/i', '/[ \-]+/'), array('', '-'), $textURL));
return $URL;
}
while($row1=mysql_fetch_array($qry)) {
<p class='news_title'><a href='newsdetail.php?id=".cleanURL($row1['news_heading'])."'>{$row1['news_heading']}</a></p>
}
The output will be
newsdetail.php?id=In-front-of-the-houses
Think so this will help you
Solution 3:[3]
You can't have raw spaces in the URL. If you try to put them in, then the browser will error correct and escape them for you.
Solution 4:[4]
use this
$url = str_replace(' ', '-', strtolower($news_heading));
replace the space of this function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vel |
Solution 2 | |
Solution 3 | Quentin |
Solution 4 | Suraj Rao |