Best Way to Escape and Create a Slug

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.

URL Slugify algorithm in C#?

http://predicatet.blogspot.com/2009/04/improved-c-slug-generator-or-how-to.html

public static string GenerateSlug(this string phrase) 
{
string str = phrase.RemoveAccent().ToLower();
// invalid chars
str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
// convert multiple spaces into one space
str = Regex.Replace(str, @"\s+", " ").Trim();
// cut and trim
str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim();
str = Regex.Replace(str, @"\s", "-"); // hyphens
return str;
}

public static string RemoveAccent(this string txt)
{
byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt);
return System.Text.Encoding.ASCII.GetString(bytes);
}

SEO Friendly links, js and/or php stripping

What you want is the "slugged" string. Here's a list of relevant links:

  • best way to escape and create a slug
  • Convert any title to url slug and back from url slug to title
  • how to generate slugs in php
  • http://snipplr.com/view/2809/convert-string-to-slug/

Just google PHP slug for more examples.

Regex Capture first slug in the URL don't capture if it have more that one slug/path

Capture everything that doesn't include a slash, and allow it only to have an optional trailing slash before the end of the string.

Regex

/example\.com\/path\/([^\/]+)\/?$/

https://regex101.com/r/K75aDD/1


Edit: As user3783243 mentioned, it's generally easier to use a different delimiter for your regex if it's related to paths with lots of slashes, so you don't have to escape them all. Convention is often to use a hash # or tilde ~ in these situations.

#example\.com/path/([^/]+)/?$#

ReactJS- Use slug instead of Id in the URL with React router

I would just do it with the way you have set up your route and then as you're using redux (because you said you were before edits) I would use mapStateToProps to filter or however it is you are passing the details as props.

for example:

const mapStateToProps = (state, ownProps) => {
user: state.users.items.filter(user => user.slug === ownProps.params.slug),
}

Converting Special Characters with PHP

If you want a slug, there are plenty of utilities which will do it for you, but neither htmlentities or urlencode is correct. Doctrine 1.2 included a urlizer class with a set of static functions including urilize which will accomplish the behavior you desire in a more robust manner (handles UTF-8 and unaccenting correctly, etc.)

It can be found here

If you want something less robust but far simpler:

function slugify($sluggable)
{
$sluggable = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $sluggable);
$sluggable = trim($sluggable, '-');
if( function_exists('mb_strtolower') ) {
$sluggable = mb_strtolower( $sluggable );
} else {
$sluggable = strtolower( $sluggable );
}
$sluggable = preg_replace("/[\/_|+ -]+/", '-', $sluggable);

return $sluggable;
}

That'll strip non-alphanumeric characters (but also accented characters) and make spaces, + signs, and hyphens into hyphens.

Escape parameter passed to Thymeleaf using @{} url syntax

I've dug in to the Thymeleaf source and can see what I think are a couple of bugs. The upshot is that what I'm trying to do isn't currently possible in 3.0.0.BETA02. I'm going to try and contact the Thymeleaf team to discuss the issue.

how to un-slug a title slug

Why not create a "slug" field in the table and just match on that? Store the slug in there exactly as it would be displayed in the url and do an exact match. Should solve your problem. Also when entering the slug in the database, check if a similar slug exists, if it does, add a number to the end. for instance 'my-post', 'my-post-1'.

So this would involve a little calculation at the insertion time but should solve your exact match issue.



Related Topics



Leave a reply



Submit