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.
<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.
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
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.
<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.
<?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.
<?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.
&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)
» How do I get a YouTube video thumbnail from YouTube using PHP
» How can i cache pages using php?
» PHP explained in a few lines
» Learn PHP Complete Guide - Introduction
» Learn PHP Complete Guide - Environment Setup
collections_bookmark Category :: Phpdate_range Published :: 4 years ago At: 11:45 AM
event_note Detailed Date :: May 15th, 2019
person Writer :: Code
- RECENT POSTS
How to force the browser to cache a page?
There are several ways to force a web browser to cache an HTML page:
How can i cache pages using php?
You can use the output buffering functions in PHP to cache pages. Output buffering allows you to store the output of a PHP script in a buffer, which you can then manipulate before sending it to the client.
PHP is a popular programming language that is widely used for web development. It stands for "PHP: Hypertext Preprocessor" and is a server-side scripting language. This means that it is executed on the server, rather than in the user's web browser.
HTML, or Hypertext Markup Language, is the standard markup language for creating web pages and web applications. It is used to structure and organize content on the web, and to create the basic structure and layout of a webpage.
CSS, or Cascading Style Sheets, is a stylesheet language used for describing the look and formatting of a document written in HTML. CSS is used to control the presentation of multiple web pages at once, making it a crucial tool for web developers and designers.
A JavaScript library for formatting and manipulating numbers - Numeral.js
Check out this lightweight JavaScript library used for formatting and manipulating numbers.
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.
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.
- ADVERTISEMENT