从PHP中的URL中删除%20

时间:2018-11-21 06:05:26

标签: php urlencode urldecode

这是我的整个代码。 在网址中用-替换%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
      }
    ?>

1 个答案:

答案 0 :(得分:0)

%20提示该字符串很可能是Url编码的(%20是单个空格字符)。因此,您可能应该做的是首先尝试对字符串进行Url解码(然后您将获得原始字符串)。然后,您可以删除/替换不需要的任何字符。

因此,在您的问题中,您说过不要%20(空格),而是连字符(-)。这可以通过以下方式实现...

  <?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对浏览器中回显的内容进行编码。

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