使用Php Get方法在URL中更新空间到连字符

使用Php Get方法在URL中更新空间到连字符

问题描述:

I would like to update space to hyphen in my php url file via get method. I have tried some methods, but i got an error.

For an example, the original url is pindtrict.php?ste=Tamil Nadu

i want optimized url as, pindtrict.php?ste=Tamil-Nadu

How to do this? Here it is my code,

<?php
include(config.php');
$state_content= "";
$qry = "SELECT DISTINCT State_N FROM pin_data ORDER BY State_N ASC";
$result = mysql_query($qry);
while($row = mysql_fetch_array($result))
    {
        $state_content .= "<li><a href='pindtrict.php?ste=".$row['State_N']."'> ".$row['State_N']."</a></li>";
    }
mysql_close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pincode Directory</title>
<body>
<div>
    <?php echo $state_content; ?><br />
</div>
</body>
</html>

我想通过get方法将我的php url文件中的空格更新为连字符。 我尝试过一些方法,但是我遇到了错误。 p>

例如, 原始网址是 pindtrict.php?ste = Tamil Nadu code> p>

我想要优化的网址 as, pindtrict.php?ste = Tamil-Nadu code> p>

如何做到这一点? 这是我的代码, p>

 &lt;?php 
include(config.php'); 
 $ state_content =“”; 
 $ qry =“SELECT DISTINCT State_N  FROM pin_data ORDER BY State_N ASC“; 
 $ result = mysql_query($ qry); 
while($ row = mysql_fetch_array($ result))
 {
 $ state_content。=”&lt; li&gt;&lt; a href =  'pindtrict.php?ste =“。$ row ['State_N']。”'&gt;“。$ row ['State_N']。”&lt; / a&gt;&lt; / li&gt;“; 
} 
mysql_close(  ); 
?&gt; 
&lt;!DOCTYPE html&gt; 
&lt; html lang =“en”&gt; 
&lt; head&gt; 
&lt; title&gt; Pincode目录&lt; / title&gt; 
&lt; body&gt; 
&lt; div&gt;  ; 
&lt;?php echo $ state_content;  ?&gt;&lt; br /&gt; 
&lt; / div&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
  code>  pre> 
  div>

Use

$state_content .= "<li><a href='pindtrict.php?ste=".str_replace(' ', '-',$row['State_N'])."'> ".$row['State_N']."</a></li>";

Anything that goes in the url as a query parameter needs to be urlencoded using urlencode(). For replacing part of string, use str_replace

"<li><a href='pindtrict.php?ste=".urlencode(str_replace(' ', '-',$row['State_N']))."'> ".$row['State_N']."</a></li>";