How to create smart Urls using PHP
Make money for being or staying online/internet.

You will get a $50 starting gift when you join using this code: Exode4LKrbujm1z and link:: GET THE OFFER NOW!!

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

Save up to 80% with this Domain & Shared Hosting package deal! 80% OFF - GET OFFER NOW

Related Post(s)

» Learn PHP Complete Guide - Introduction

» How to create smart Urls using PHP

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

» Quick Autocomplete App With PHP and JQUERY MOBILE

» Learn PHP Complete Guide - Environment Setup

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

Mr.

(sel


3 weeks ago

Mr.

(sel


3 weeks ago

Mr.

555


3 weeks ago

Mr.

555


3 weeks ago

Mr.

5550


3 weeks ago

Mr.

5550


3 weeks ago

Mr.

555


3 weeks ago

Mr.

555


Subscribe
  • ADVERTISEMENT

YOU MAY LIKE THESE POSTS

share