PHP function to make slug (URL string)
Instead of a lengthy replace, try this one:
public static function slugify($text, string $divider = '-')
{
// replace non letter or digits by divider
$text = preg_replace('~[^\pL\d]+~u', $divider, $text);
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
// trim
$text = trim($text, $divider);
// remove duplicate divider
$text = preg_replace('~-+~', $divider, $text);
// lowercase
$text = strtolower($text);
if (empty($text)) {
return 'n-a';
}
return $text;
}
This was based off the one in Symfony's Jobeet tutorial.
PHP to convert string to slug
Here is a modified function that I originally found here (http://cubiq.org/the-perfect-php-clean-url-generator). you can pass ''
as the delimiter to not use '-'
.
public static function createSlug($str, $delimiter = '-'){
$slug = strtolower(trim(preg_replace('/[\s-]+/', $delimiter, preg_replace('/[^A-Za-z0-9-]+/', $delimiter, preg_replace('/[&]/', 'and', preg_replace('/[\']/', '', iconv('UTF-8', 'ASCII//TRANSLIT', $str))))), $delimiter));
return $slug;
}
String to SEO-friendly slug
You may use this code with preg_replace
instead of str_replace
to do replacements using a regex.
$s = 'I & GN Hospital and Nurses' Quarters';
echo preg_replace("/[\h&(),'-]+/", '-', preg_replace('/\h*\([^)]*\)/', '', $s));
//=> I-GN-Hospital-and-Nurses-Quarters
$p = 'Mary Kate Hunter (November 8, 1866 - April 15, 1945)';
echo preg_replace("/[\h&(),'-]+/", '-', preg_replace('/\h*\([^)]*\)/', '', $p));
//=> Mary-Kate-Hunter
Here:
preg_replace('/\h*\([^)]*\)/', '', $s)
: Strips out(...)
text from inputpreg_replace("/[\h&(),'-]+/", '-', $str)
: Replaces all chosen characters with-
Convert any title to url slug and back from url slug to title
You want to create slugs, but from experience i can tell you the decode possibilities are limited. For example "Foo - Bar" will become "foo-bar" so how do you then can possibly know that it wasn't "foo bar" or "foo-bar" all along?
Or how about chars that you don't want in your slug and also have no representation for like " ` "?
So you can ether use a 1 to 1 converstion like rawurlencode()
or you can create a Slug, here is an example for a function - but as i said, no reliable decoding possible - its just in its nature since you have to throw away Information.
function sanitizeStringForUrl($string){
$string = strtolower($string);
$string = html_entity_decode($string);
$string = str_replace(array('ä','ü','ö','ß'),array('ae','ue','oe','ss'),$string);
$string = preg_replace('#[^\w\säüöß]#',null,$string);
$string = preg_replace('#[\s]{2,}#',' ',$string);
$string = str_replace(array(' '),array('-'),$string);
return $string;
}
Creating a slug in the URL when using PHP String to pull data
If you're using Wordpress, take a look at add_rewrite_rule(). Along with add_rewrite_tag, this will allow you to handle that.
What you can do is define a rewrite tag for user_id, and a rewrite rule to map your pattern to the single page that handles it. You can then get the value using query_vars eg.
$wp_query->query_vars['user_id']
So this would return eg. john-doe. On your database you would have an index for the lawyer's permalink which you can then look up to find their id/record.
It's a bit confusing at first, but there is a decent example in the doc linked above.
Related Topics
How to Solve "Fatal Error: Class 'MySQLi' Not Found"
Is Closing the MySQL Connection Important
How to Return Outer HTML of Domdocument
Convert Flat Array to a Delimited String to Be Saved in the Database
Detect Language from String in PHP
How to Put Composite Keys in Models in Laravel 5
Achieve Hierarchy, Parent/Child Relationship in an Effective and Easy Way
PHP Multi-Dimensional Array Remove Duplicate
I Need My PHP Page to Show My Blob Image from MySQL Database
Detect Bad Json Data in PHP Json_Decode()
How to Pass Data to View in Laravel
"Invalid Parameter Number: Parameter Was Not Defined" Inserting Data
Performance of For VS Foreach in PHP
Fatal Error: Call to Undefined Function MySQL_Connect()
How to Get the Error Message For the Mail() Function