February 15, 2024 5:05 am

How to create SEO Optimized URLs from String in PHP

A uniform resource locator abbreviated URLs or web address used to locate exact information page on the web like https://www.phpgang.com/how-to-create its a URL  and how-to-create is  created from a string and in this tutorial I will give you a method to know about that function of PHP. This is a very basic solution for beginner PHP Developers and programmers.

pretty-urls-from-strings

Here is the code snippet to create URLs from a String.

function.php

<?php
function to_prety_url($str){
	if($str !== mb_convert_encoding( mb_convert_encoding($str, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32') )
		$str = mb_convert_encoding($str, 'UTF-8', mb_detect_encoding($str));
	$str = htmlentities($str, ENT_NOQUOTES, 'UTF-8');
	$str = preg_replace('`&([a-z]{1,2})(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i', '\1', $str);
	$str = html_entity_decode($str, ENT_NOQUOTES, 'UTF-8');
	$str = preg_replace(array('`[^a-z0-9]`i','`[-]+`'), '-', $str);
	$str = strtolower( trim($str, '-') );
	return $str;
}
?>

To use this function you have to create this page index.php which contain function call line with parameters.

<?php
include('function.php');
$str = "How to Create HTML5 Form"; // this string will be submitted in parameter.
echo to_prety_url($str); // show optimized url string.
?>

This function first of all convert string to utf-8 after that use perg_replace and change white spaces to “-” and remove special characters.

These URLs also make good impact on your SEO and recommended by Google.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

14 responses to “How to create SEO Optimized URLs from String in PHP”

  1. Yogi Singh says:

    ok, but how will it reflect the string to address bar

    • huzoorbux says:

      @disqus_xCJR1jcNNv:disqus Changes added in article how to use it..

      • Yogi Singh says:

        ok when i get the string, i can save in db or use as permalink
        but how will it show in address bar in top. i am working on core php no frameworks.
        what changes to be made in htaccess

  2. Toreto says:

    looks good – but how to use it ? By include ?
    Thanx

  3. Agus Sumarna says:

    Thank you. Nice info

  4. Lokesh Sharma says:

    hello Sir give me some ideas about SEO Please

  5. Lokesh Sharma says:

    help Me Sir

  6. Shahbaz Ahmed Bhatti says:

    i need some help in page url can anybody help me. phpGang team i need some help please

    • huzoorbux says:

      What help you need?

      • Shahbaz Ahmed Bhatti says:

        Asalamu Alaikaum,
        Dear HuzorBux Sir
        i m new in Web Development. i Found your site very late it is very nice and good for learning. The Help i need is that

        Can PhpGang Team Help me in Page URL a si asked yesterday
        Let sya i have a page url is
        sitenamecom/recird .php?id=12&city=37

        i want like this sitenamecom/record .php

        hope u will understoood

  7. Md Asif says:

    As Salaam O Aleykum Huzoor Sir. May you please publish an article on building blog with all the facilities. like recent post, categories & archives.

  8. Jitendra Nagar says:

    hello sir i want userfriendy url without usig httaccess file and webconfig in php

Leave a Reply

Your email address will not be published. Required fields are marked *