从 php 中的 url 移除 %20

remove %20 from url in php

这是我的全部代码.... 我在 url 中用 - 替换 %20 时遇到问题。我尝试了很多代码但都失败了。请帮帮我。我附上我的完整代码。我在

中遇到问题

/">访问

我在这里同时显示 ID 和公司名称。我只想显示 id 和公司名称,它们由 /

分隔
<?php 
error_reporting("0");
 $rowperpage =10 ;
@$select_city=$_GET['city'];
if(isset($_REQUEST['search'])){

 $allcount_query = "SELECT count(*) as allcount FROM inventory_details";

            $allcount_result = mysqli_query($conn, $allcount_query);
            $allcount_fetch = mysqli_fetch_array($allcount_result);
            $allcount = $allcount_fetch['allcount'];

 $sql="SELECT * FROM inventory_details";


   $search_item = mysqli_real_escape_string($conn, $_REQUEST['search_box']);

   $sql .= " WHERE (firm_name LIKE '%$search_item%'";
    $sql .= " OR catagory_name LIKE'%$search_item%'";
    $sql .= " OR mobile_no LIKE'%$search_item%'";
      $sql .= " OR product_key LIKE'%$search_item%')";
       $sql .= " AND city='$select_city' AND status='0' ";
       $sql .="ORDER BY today_date DESC limit 0,$rowperpage";

       // $sql .=  " ORDER BY catagory_name LIMIT 0,5";
       // echo $sql;
     if($_GET['search_box']==""){
      $msg=include"error_msg.php";
     }

   else{
     $fquery=mysqli_query($conn, $sql); 
     $totalrec=mysqli_num_rows($fquery);

   if ($totalrec=='0'){
    $msg=include"error_msg.php";
   }
   }

}
//   if($selct_city=$_GET['city']){
// SELECT * FROM inventory_details WHERE city LIKE '%DELHI%' && catagory_name LIKE '%belts%'
//    SELECT * FROM inventory_details WHERE firm_name='keyboard' OR catagory_name='keyboard' OR mobile_no='keyboard' OR city='keyboard' OR product_key='keyboard' && city="delhi"
//   echo $sql . $fquery;
// SELECT * FROM inventory_details WHERE city='delhi' AND catagory_name like '%foot%' OR firm_name LIKE'%foot%' OR product_key like '%foot%' OR mobile_no like '%foot%'
// SELECT * FROM inventory_details WHERE city='Delhi' AND firm_name LIKE '%foot%' OR catagory_name LIKE'%foot%' OR mobile_no LIKE'%foot%' OR product_key LIKE'%foot%'
// // }

 ?>
<?php 

?>


  <!-- Brand and toggle get grouped for better mobile display -->
<!--    <div class="container">
    <form class="navbar-form" role="search">
        <div class="input-group col-md-12 col-xs-12  ">
           <div class=" col-md-offset-7 col-md-5">

           <select class="form-control  select country" name="city" required="">please select city
                             <?php ;?>
                 </select>
              <select class="form-control input-lg" placeholder="Search By Firm name,Category name,Mobile no....." name="search_box">
              <option>select city</option>
              <option>Delhi</option>
              <option>Mumbai</option>
              <option>Channai</option>
              <option>Kolkata</option>
              </select>
            </div>
            <div class="input-group-btn ">

            </div>
            <div class=" col-md-12">
            <input type="text" class="form-control" placeholder="Just Type Anything..." name="search_box">
            </div>
            <div class="input-group-btn ">
                <a href="catagory.php"><button class="btn btn-info" type="submit" name="search"><span class=""></span>search</button></a>
            </div>
        </div>
        </form>
        </div> -->
 <?php 

   while($row=mysqli_fetch_assoc(@$fquery)){  //handle rows.

      $id=$row['id'];
     $catagory_name=$row['catagory_name'];
     $firm_name=$row['firm_name'];
   // $user_pass=md5($_POST ['user_pass']);
     $city=$row ['city'];
      $product_key=$row ['product_key'];
      $firm_email=$row ['firm_email'];
      $mobile_no=$row ['mobile_no'];
      $phone_no=$row ['phone_no'];
      $address=$row ['address'];
      $Fax_no=$row ['Fax_no'];
      $Website=$row ['Website'];
      $product_key=$row['product_key'];
       $sotime=$row ['sotime'];
      $sctime=$row ['sctime'];
      $Contact_person=$row ['Contact_person'];
      $Contact_person_mobile=$row ['Contact_person_mobile'];
      $colorname=$row ['colorname'];
      $textcolor=$row ['textcolor'];

      $compLogo=$row ['compLogo'];

      ////////////////////////////////////////
      $banner=$row ['banner'];
     if(empty($banner))
     {
    $banner="default.jpg"; 
     }
?>

<div class="container post" id="post_<?php echo $id; ?>">

 <div class="col-md-12 inv_data" style="background:<?php echo $colorname; ?>; color:<?php echo $textcolor; ?>;">

 <div class="col-md-3">
 <!-- <?php  ?>
   <img src="image/banner/<?php echo $banner; ?>" class="img_style"> -->
   <?php

       $imageArr = explode(',',$compLogo);
       foreach ($imageArr as $k=>$val) { if($val!=""){
     ?>

    <img class="mySlides" src="image/logo/<?php echo $val; ?>">
     <?php
   }}
     ?>
  <img class="mySlides" src="image/logo/defualt.jpg">
</div>

  <div class="col-md-7">
    <h3><strong><?php echo strtoupper($firm_name); ?></strong></h3>
      <strong></strong> <?php echo $product_key; ?><br>
          <strong> <span class="glyphicon glyphicon-home"></span> </strong> <?php echo $address; ?><br>
         <!--  <strong>Category : -</strong> <?php echo $catagory_name." , " . $product_key;?><br> -->
          <!-- <strong>Phone no : -</strong> <?php echo $phone_no; ?>&nbsp;
          <strong>Mobile no : -</strong> <?php echo $mobile_no; ?>&nbsp;

          <br>
        <strong> Whatsapp no : -</strong> <?php echo $Contact_person_mobile ; ?>&nbsp;
        <strong> Contact Person : -</strong> <?php echo $Contact_person ; ?>&nbsp;

        <br>
          <strong>Email : -</strong> <?php echo $firm_email;?>&nbsp;
          <strong>Timing : -</strong> <?php echo $sotime. " to ".$sctime?><br>

          <strong>Website : -</strong> <?php echo $Website;?><br> -->
  </div>
  <div class="col-md-2">
  <span>&nbsp;<br>&nbsp;<br>&nbsp;<br></span>
   <a href="category.php?edit=<?php echo $id;?>/<?php echo $firm_name;?>"><button type="button" class="btn btn-info btn-lg">Visit</button></a> 

   <!--<a href="/jyp/<?php echo $id;?><?php echo $firm_name;?>"><button type="button" class="btn btn-info btn-lg">Visit</button>-->
   </a>
  </div>

 </div>
</div>
 <?php

}

 ?>
  <?php 
  if(isset($_REQUEST['search'])){
    ?>

  <h1 id="loadbtn"><span class="load-more">Load More</span></h1>
    <input type="hidden" id="row" value="0">
   <input type="hidden" id="cityname"  name="cityname" value="<?php echo  $city; ?>">
   <input type="hidden" id="search_item"  name="search_item" value="<?php echo $search_item; ?>">
    <input type="hidden" id="all" value="<?php echo $allcount; ?>">

    <?php
      }
    ?>

%20 提示该字符串很可能是 Url 编码的(%20 是单个 space 字符)。所以你可能应该做的是首先尝试 Url 解码字符串(然后你将得到原始字符串)。然后你可以 remove/replace 任何你不想要的字符。

所以在你的问题中,你说你不想要 %20(一个 space)而是一个破折号 (-)。这可以通过以下方式实现...

  <?php 
    error_reporting("0");
     $rowperpage =10 ;
    @$select_city=$_GET['city'];
    if(isset($_REQUEST['search'])){

     $allcount_query = "SELECT count(*) as allcount FROM inventory_details";

                $allcount_result = mysqli_query($conn, $allcount_query);
                $allcount_fetch = mysqli_fetch_array($allcount_result);
                $allcount = $allcount_fetch['allcount'];

     $sql="SELECT * FROM inventory_details";


       $search_item = mysqli_real_escape_string($conn, $_REQUEST['search_box']);

       $sql .= " WHERE (firm_name LIKE '%$search_item%'";
        $sql .= " OR catagory_name LIKE'%$search_item%'";
        $sql .= " OR mobile_no LIKE'%$search_item%'";
          $sql .= " OR product_key LIKE'%$search_item%')";
           $sql .= " AND city='$select_city' AND status='0' ";
           $sql .="ORDER BY today_date DESC limit 0,$rowperpage";

           // $sql .=  " ORDER BY catagory_name LIMIT 0,5";
           // echo $sql;
         if($_GET['search_box']==""){
          $msg=include"error_msg.php";
         }

       else{
         $fquery=mysqli_query($conn, $sql); 
         $totalrec=mysqli_num_rows($fquery);

       if ($totalrec=='0'){
        $msg=include"error_msg.php";
       }
       }

    }
    //   if($selct_city=$_GET['city']){
    // SELECT * FROM inventory_details WHERE city LIKE '%DELHI%' && catagory_name LIKE '%belts%'
    //    SELECT * FROM inventory_details WHERE firm_name='keyboard' OR catagory_name='keyboard' OR mobile_no='keyboard' OR city='keyboard' OR product_key='keyboard' && city="delhi"
    //   echo $sql . $fquery;
    // SELECT * FROM inventory_details WHERE city='delhi' AND catagory_name like '%foot%' OR firm_name LIKE'%foot%' OR product_key like '%foot%' OR mobile_no like '%foot%'
    // SELECT * FROM inventory_details WHERE city='Delhi' AND firm_name LIKE '%foot%' OR catagory_name LIKE'%foot%' OR mobile_no LIKE'%foot%' OR product_key LIKE'%foot%'
    // // }

     ?>
    <?php 

    ?>


      <!-- Brand and toggle get grouped for better mobile display -->
    <!--    <div class="container">
        <form class="navbar-form" role="search">
            <div class="input-group col-md-12 col-xs-12  ">
               <div class=" col-md-offset-7 col-md-5">

               <select class="form-control  select country" name="city" required="">please select city
                                 <?php ;?>
                     </select>
                  <select class="form-control input-lg" placeholder="Search By Firm name,Category name,Mobile no....." name="search_box">
                  <option>select city</option>
                  <option>Delhi</option>
                  <option>Mumbai</option>
                  <option>Channai</option>
                  <option>Kolkata</option>
                  </select>
                </div>
                <div class="input-group-btn ">

                </div>
                <div class=" col-md-12">
                <input type="text" class="form-control" placeholder="Just Type Anything..." name="search_box">
                </div>
                <div class="input-group-btn ">
                    <a href="catagory.php"><button class="btn btn-info" type="submit" name="search"><span class=""></span>search</button></a>
                </div>
            </div>
            </form>
            </div> -->
     <?php 

       while($row=mysqli_fetch_assoc(@$fquery)){  //handle rows.

          $id=$row['id'];
         $catagory_name=$row['catagory_name'];
         $firm_name=$row['firm_name'];
       // $user_pass=md5($_POST ['user_pass']);
         $city=$row ['city'];
          $product_key=$row ['product_key'];
          $firm_email=$row ['firm_email'];
          $mobile_no=$row ['mobile_no'];
          $phone_no=$row ['phone_no'];
          $address=$row ['address'];
          $Fax_no=$row ['Fax_no'];
          $Website=$row ['Website'];
          $product_key=$row['product_key'];
           $sotime=$row ['sotime'];
          $sctime=$row ['sctime'];
          $Contact_person=$row ['Contact_person'];
          $Contact_person_mobile=$row ['Contact_person_mobile'];
          $colorname=$row ['colorname'];
          $textcolor=$row ['textcolor'];

          $compLogo=$row ['compLogo'];

          ////////////////////////////////////////
          $banner=$row ['banner'];
         if(empty($banner))
         {
        $banner="default.jpg"; 
         }
    ?>

    <div class="container post" id="post_<?php echo $id; ?>">

     <div class="col-md-12 inv_data" style="background:<?php echo $colorname; ?>; color:<?php echo $textcolor; ?>;">

     <div class="col-md-3">
     <!-- <?php  ?>
       <img src="image/banner/<?php echo $banner; ?>" class="img_style"> -->
       <?php

           $imageArr = explode(',',$compLogo);
           foreach ($imageArr as $k=>$val) { if($val!=""){
         ?>

        <img class="mySlides" src="image/logo/<?php echo $val; ?>">
         <?php
       }}
         ?>
      <img class="mySlides" src="image/logo/defualt.jpg">
    </div>

      <div class="col-md-7">
        <h3><strong><?php echo strtoupper($firm_name); ?></strong></h3>
          <strong></strong> <?php echo $product_key; ?><br>
              <strong> <span class="glyphicon glyphicon-home"></span> </strong> <?php echo $address; ?><br>
             <!--  <strong>Category : -</strong> <?php echo $catagory_name." , " . $product_key;?><br> -->
              <!-- <strong>Phone no : -</strong> <?php echo $phone_no; ?>&nbsp;
              <strong>Mobile no : -</strong> <?php echo $mobile_no; ?>&nbsp;

              <br>
            <strong> Whatsapp no : -</strong> <?php echo $Contact_person_mobile ; ?>&nbsp;
            <strong> Contact Person : -</strong> <?php echo $Contact_person ; ?>&nbsp;

            <br>
              <strong>Email : -</strong> <?php echo $firm_email;?>&nbsp;
              <strong>Timing : -</strong> <?php echo $sotime. " to ".$sctime?><br>

              <strong>Website : -</strong> <?php echo $Website;?><br> -->
      </div>
      <div class="col-md-2">
      <span>&nbsp;<br>&nbsp;<br>&nbsp;<br></span>


       <!-- there's the solution below -->

       <?php
            $firm_name = urldecode($firm_name);    # decode the string (like converting %20 to a space character)
            $firm_name = str_replace(' ', '-', $firm_name);    # then replace any characters you don't want (like converting a space character to a -
            $url = "category.php?edit=" . urlencode($id) . '/' . urlencode($firm_name);
       ?>

       <a href="<?php echo $url ?>"><button type="button" class="btn btn-info btn-lg">Visit</button></a> 

       <?php
        // probably don't need these now.
        $firm_name = urldecode($firm_name);  
        $firm_name = str_replace(' ', '-', $firm_name); 

                        # are you missing a / here ???
        $url = '/jyp/' . urlencode($id) . '' . urlencode($firm_name);
   ?>

   <a href="<?php echo $url ?>"><button type="button" class="btn btn-info btn-lg">Visit</button>
   </a>
      </div>

     </div>
    </div>
     <?php

    }

     ?>
      <?php 
      if(isset($_REQUEST['search'])){
        ?>

      <h1 id="loadbtn"><span class="load-more">Load More</span></h1>
        <input type="hidden" id="row" value="0">
       <input type="hidden" id="cityname"  name="cityname" value="<?php echo  $city; ?>">
       <input type="hidden" id="search_item"  name="search_item" value="<?php echo $search_item; ?>">
        <input type="hidden" id="all" value="<?php echo $allcount; ?>">

        <?php
          }
        ?>

您可能应该使用 htmlspecialchars.

对您回显给浏览器的任何内容进行编码

免责声明:我测试 PHP 代码的网站在使用 rawurldecode('hi%20you');urldecode('hi%20you'); 时给出了相同的结果。