C

codeguru

Member
Freecoin
295
In this tutorial, we’ll show you how to generate SEO friendly URL from string using PHP. Our example script makes it easy to convert title string to SEO friendly URL in PHP. We’ve grouped together all the PHP code into a function named generateSeoURL(). The generateSeoURL() function automatically create clean and SEO friendly URL slug from string.

Create SEO Friendly URL in PHP​

The generateSeoURL() function automatically creates SEO friendly URL slug from the string using PHP. A title string needs to be passed as input and it returns a human-friendly URL string with a hyphen (-) as the word separator.
  • $string – Required. The string which you want to convert to the SEO friendly URL.
  • $wordLimit – Optional. Restrict words limit on SEO URL, default is 0 (no limit)

    function generateSeoURL($string, $wordLimit = 0){
    $separator = '-';

    if($wordLimit != 0){
    $wordArr = explode(' ', $string);
    $string = implode(' ', array_slice($wordArr, 0, $wordLimit));
    }

    $quoteSeparator = preg_quote($separator, '#');

    $trans = array(
    '&.+?;' => '',
    '[^\w\d _-]' => '',
    '\s+' => $separator,
    '('.$quoteSeparator.')+'=> $separator
    );

    $string = strip_tags($string);
    foreach ($trans as $key => $val){
    $string = preg_replace('#'.$key.'#iu', $val, $string);
    }

    $string = strtolower($string);

    return trim(trim($string, $separator));
    }

Uses

Use the generateSeoURL() function to generate SEO friendly URL from title string using PHP. Provide the article title or string in the first parameter of generateSeoURL() function. If you want to restrict words on the URL, provide the number of the words otherwise can omit it.
Without Words Limit:

$postTitle = 'Generate SEO Friendly URL from String in PHP';

$seoFriendlyURL = generateSeoURL($postTitle);

// Output will be: generate-seo-friendly-url-from-string-in-php

With Words Limit:

$postTitle = 'Generate SEO Friendly URL from String in PHP';

$seoFriendlyURL = generateSeoURL($postTitle, 4);

// Output will be: generate-seo-friendly-url

Should you have alternative method of getting this done, kindly quote me
 
W

webguru

New member
Freecoin
0
This seems complicated, is there a simple method code that i can add in .htaccess
 

Richest Freecoded User

Most Freecoin

freecoded
freecoded
2,581 Freecoin
Davy200
Davy200
590 Freecoin
nathan69
nathan69
424 Freecoin
Laureine
Laureine
415 Freecoin
C
codeguru
295 Freecoin
Tekera
Tekera
263 Freecoin
R
ruthailand
221 Freecoin
A
Akubay
170 Freecoin
smitha
smitha
104 Freecoin
G
Gabby
93 Freecoin
Top