ausi / slug-generator

Slug Generator Library for PHP, based on Unicode’s CLDR data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Verify if slug is valid.

daebak74 opened this issue · comments

There is a reverse method for verify if a typed slug is valid ?

Thanks.

Currently there is no such method. But you can use something like the following code to check if a string is a valid slug:

if ($generator->generate($inputString) === $inputString) {
	// Valid
} else {
	// Invalid
}

Does this solve your use case?

Thanks for your reply.
Not exactly make an example in a form we have 2 fields editable something like contao:

Title and Alias

I use Title if I wanna auto generate the alias from title ok I call $generator->generate('title') and I am sure that the alias is correct.

But alias is editable so I can change it and before to store in db I need to check if the input alias is correct.

Or I can make a control directly on alias value I don't know if is correct.

$alias = this-is-my-alias

if ($generator->generate($alias) === $alias) {
// Valid
} else {
// Invalid
}

Hope that my example is clear.

Yes, validating if the input alias is correct can be done with the mentioned code.

You could also create a verify function yourself:

function verifySlug($slug): bool {
	$generator = new SlugGenerator();
	return $generator->generate($alias) === $alias;
}

This function returns true for verifySlug('valid-alias') and false for verifySlug('Title Foöbär').

Great. Thanks! :)