How to create smart Urls using PHP

In this post, you will learn about creating Clean URL’s in PHP without using the Apache htaccess file on your server.

Having clean URLs will enable search engines and other websites to link to your website easily. Due to this, they are very important.

Note: we are only going to clean up the title parameter passed into the url.

Wait,What is a URL?

This is a reference to a web resource that specifies its location on a computer network and a mechanism for retrieving it.

In short terms,it's the address of a World Wide Web page.

URL in full
  • U - Uniform
  • R - Resource
  • L - Locator

Ok back to the topic, by default when you fetch data from the database it is returned the way it was stored. For this case lets concentrate on the links.

Example

Take note of the title parameter, The actual title is returned back from the database with it's original spaces every after single character.

copy

<a href="https://www.example.com/details?id=108&title=How to create a website">
How to create a website
</a>

So when users click on such a link, this is how it will be displayed in the browser web address.The title parameter will contain %20 characters which we don't want.

copy

	https://www.example.com/details?id=108&title=How%20to%20create%20a%20website

	//title parameter
	title=How%20to%20create%20a%20website

Good news, with PHP we can strip out those characters(%20) by using the str_replace function.

Usage

The str_replace() function replaces some characters with some other characters in a string.

Syntax
copy

	str_replace(search, replace, subject);

Parameter Description
search Required. Specifies the value to find
replace Required. Specifies the value to replace the value in search.
subject Required. Specifies the string to be searched.

Absorbing this amount of content would be a lot if you are just reading it, So I have put together a very simple video for you to understand the logic.

Project source code

The file you will download contains 3 files,namely;

  • index.php
  • post.php
  • url_convertor.php
This is the index.php file.
copy

<html>

<head>
 <title>Smart Url</title>
</head>
<body>

<?php 
	//includes the file to clean on the title parameter
	require_once 'url_convertor.php';

	//fake data to use
	$id = 100;
	$Article_title = "How to create a website";
?>


<p>
	<a href="post.php?id=<?php echo $id; ?>&title=<?php echo UrlConvertor($Article_title); ?>">
		<?php echo $Article_title; ?>
	</a>
</p>
 
</body>
</html>

This is the post.php file.

This file is actually pulling the title parameter value from the web address and displaying to the browser.

copy

<?php
	if(isset($_GET['title'])){
		$title = $_GET['title'];

		echo $title;
	}

 ?>

This is the url_convertor.php file.

This is a file that handles all the logic for making our title parameter clean.

copy

<?php

	function UrlConvertor($title){

		$url = str_replace(' ', '-',$title);

		return $url;
	}

?>

Since we included the url_convertor.php file in the index.php file. We get access to the UrlConvertor function.

To get a clean url title parameter, we wrap the Article_title from the database inside the UrlConvertor function like this.

copy

&title=<?php echo UrlConvertor($Article_title); ?>

To download the source code: Click here description

Related Post(s)

» How do I get a YouTube video thumbnail from YouTube using PHP

» Learn PHP Complete Guide - Environment Setup

» Quick Autocomplete App With PHP and JQUERY MOBILE

» How to create smart Urls using PHP

» How to convert A Timestamp in Date Format

collections_bookmark Category :: Php
date_range Published :: 2 years ago At: 11:45 AM
event_note Detailed Date :: May 15th, 2019
person Writer :: Code
  • RECENT POSTS
3 months ago

A JavaScript library for formatting and manipulating numbers - Numeral.js

Check out this lightweight JavaScript library used for formatting and manipulating numbers.


4 months ago

All Countries Drop Down List | HTML Select Country Name

This simple country dropdown list is freely available for you to copy and use in your project forms.


4 months ago

HTML Entities Code Alphabet Discovery Using JavaScript

In this post I will show how writing just a few lines in JavaScript will allow you to render, browse and discover the alphabetical letters using a set of HTML entity codes.


4 months ago

YTS YIFY MOVIES API - PROJECT PREVIEW

Download YTS YIFY movies using this simple project I created using their API.


4 months ago

Display Random Advice On Your Website or Web App Using Advice Slip JSON API

Here is a short project to display the use of APIs. I decided to work with an advice API because it seemed like a fun one that I haven't worked on before.


6 months ago

How do I get a YouTube video thumbnail from YouTube using PHP

Have you ever been on youtube and admired a thumbnail of a video?


8 months ago

How to recreate Facebook's console warning

When I was inspecting some code on Facebook. I came across this interesting feature. A simple styled console warning message from Facebook to its dear users(products).


11 months ago

How To Create A Stacked Gallery Using HTML And CSS Source Code

In this post I provide you with the source code for the youtube tutorial I made on how to create a Stacked image gallery Using HTML and CSS.


Subscribe
  • ADVERTISEMENT

YOU MAY LIKE THESE POSTS

share