Tam pagelbės funkcija
$slug = preg_replace("/-$/","",preg_replace('/[^a-z0-9]+/i', "-", strtolower($title)));
Just hit the database once, grab everything at once, chances are that's the biggest bottleneck.
$query = "SELECT * FROM $table_name WHERE $field_name LIKE '".$slug."%'";
Then put your results in an array (let's say
$slugs
)//we only bother doing this if there is a conflicting slug already
if(mysqli_num_rows($result) !== 0 && in_array($slug, $slugs)){
$max = 0;
//keep incrementing $max until a space is found
while(in_array( ($slug . '-' . ++$max ), $slugs) );
//update $slug with the appendage
$slug .= '-' . $max;
}
We use the
in_array()
checks as if the slug was my-slug
the LIKE
would also return rows such asmy-slug-is-awesome
my-slug-is-awesome-1
my-slug-rules
šaltinis: http://stackoverflow.com/questions/15971685/creating-unique-page-title-slugs-php
Komentarų nėra:
Rašyti komentarą