php - Replace whitespace with hyphen in URL -
i trying create seo friendly site. @ moment page works because id shown in url product.php?id=4
want replace id
product_name
, value can contain whitespaces, url looks product.php?product_name=fence%20panel
doesn't work because use variable product details in database, , when thinks 1 word no product found. think need variable adds hyphen product.php?product_name=fence-panel
variable changes hyphen space. correct me if i'm wrong
if(isset($_get['id'])){ $id = $_get['id']; //get product if exists //if no product give message $sql = mysqli_query($mynewconnection, "select * products id='$id' limit 1"); $productcount = mysqli_num_rows($sql); if ($productcount > 0) { //get product details while($row = mysqli_fetch_array($sql)){ $product_name = $row["product_name"]; $price = $row["price"]; $details = $row["details"]; $category = $row["category"]; $date_added = strftime("%b %d, %y", strtotime($row["date_added"])); $_session['product_price_array'] = $price; } } else{ echo "no product in system"; exit(); }
use rawurlencode()
, rawurldecode()
. see the php documentation examples. won't add hyphens want. add hyphens try doing following after encoding: $str = str_replace('-', '%2d', $str);
. , should once again , swap 2 first parameters before decoding, too!
Comments
Post a Comment