doutuzhuohao6449 2012-11-21 19:04
浏览 38
已采纳

PHP函数生成干净的URL - 撇号

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...

  • 写回答

3条回答 默认 最新

  • duanbi3151 2012-11-22 11:14
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向