Pregunta

I want to create clean URLs for my database site and I've decided that the best way is to create a field in which to store the URLs for their respective entries.

I am using the GenerateUrl function (found here) to create a clean URL from the source name:-

function GenerateUrl ($s) {

    // Convert accented characters, and remove parentheses and apostrophes
    $from = explode (',', "ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u,(,),[,],'");
    $to   = explode (',', 'c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u,,,,,,');

    // Do the replacements, and convert all other non-alphanumeric characters to spaces
    $s = preg_replace ('~[^\w\d]+~', '-', str_replace ($from, $to, trim ($s)));

    // Remove a - at the beginning or end and make lowercase
    return strtolower (preg_replace ('/^-/', '', preg_replace ('/-$/', '', $s)));
}

It works great for the most part, although I am having problems with apostrophes.

Used as quotation marks (i.e. only touching another character on one side) they work fine:- 'Eiffel Tower (Paris)' becomes the URL: eiffel-tower-paris

But used as actual apostrophes (i.e. being sandwiched between two characters), not so well:- St Paul's Cathedral (London) becomes the URL: st-paul-s-cathedral-london

I'm using PHP 5.4.3 and have code to undo the modifications of magic quotes (should this be the cause of the problem).

Any advice on solving this greatly appreciated.

Thanks,

Andy


Yes, desired URL would be: st-pauls-cathedral-london

I've just figured out the original function code I gave DOES work (apostrophes are replaced with nothing at the end of the $from and $to rows respectively).

However, it only seems to works if I apply the function to the name live on the page, i.e.

<?php echo generateurl($attraction['attraction_name']); ?>

But what I am currently doing is applying the function within the index.php file when data is entered into the website. I suspect the problem is coming from the fact that I am applying the function to a value which has already had the below function applied to it (to deal with magic quotes):-

$attraction_name = mysqli_real_escape_string($link, $_POST['attraction_name']);
$attraction_url = generateurl($attraction_name);

I reckon I've got to shift some coding around to generate the URL from the attraction_name before it is affected by mysqli_real_escape_string. I'll let you know how I get on...

¿Fue útil?

Solución 3

Yes, it turns out that 'mysqli_real_escape_string' was the cause of the problem. A bit of reordering of the code seems to have sorted it:-

$attraction_url = generateurl($_POST['attraction_name']);
$attraction_name = mysqli_real_escape_string($link, $_POST['attraction_name']);

Thanks!

Andy


How to remove 'the', 'a', and 'an' from start of any URL:-

Replace the bottom line of the above GenerateUrl function code with:-

return strtolower (preg_replace ('/^-/', '', preg_replace ('/-$/', '', preg_replace ('/\b(^the|^a|^an)\b/i', '', $s))));

Useful advice on this subject from Stack Overflow.

And good article on using \b for word boundaries in regular expressions from Regex Tutorial.

Otros consejos

If st-paul-s-cathedral-london isn't right, then I assume you wanted st-pauls-cathedral-london? In that case, I think it's best to just remove all apostrophes from the string altogether. If it's inbetween letters, it will be removed completely, so paul's becomes pauls, but if there's another character, then that character will become a dash anyway, so the apostroph still isn't needed.

I would change the last line, like shown below. It replaces the ' with nothing, and uses a more simple trim to remove the leading and trailing dash. No regular expressions needed and the code is shorter and more clear.

return trim(str_replace("'", '', strtolower($s)), '-');

Add another regexp to remove apostrophes.

$s = str_replace ($from, $to, trim ($s));
$s = preg_replace("~'~", '', $s);
$s = preg_replace ('~[^\w\d]+~', '-', $s);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top