从URL中删除撇号

时间:2015-08-24 03:11:00

标签: php mysql

我正在开发一个专注于动物的网站。网址pip install xvfbwrapper 与我的数据库中的值mysite/life/ursus-maritimus匹配。

但我也想在有机体的通用名称下显示此页面 - Ursus-maritimus

没问题,我还有另一张桌子,里面有很多名字,包括北极熊。'我只需用这样的连字符替换两个单词之间的空格:

mysite/life/polar-bear

但是像$CommonURL = str_replace('-', ' ', $MyURL); [QUERY WHERE CLAUSE] WHERE Name_Common = :CommonURL 这样的常用名称呢?如何修改我的WHERE子句,使其忽略撇号,重音符号等,显示Grevy’s zebra之类的URL?

3 个答案:

答案 0 :(得分:1)

使用str_ireplace

$MyURL="Grevy’s zebra";
$CommonURL = str_ireplace("’", "’", $MyURL);
echo $CommonURL = htmlentities($CommonURL);
//this would echo Grevy’s zebra

答案 1 :(得分:1)

好吧,如果我理解正确,你想要删除所有 自然字符;如果是这样,那么你可以这样做:(代码评论很好:))

<?php
echo '<pre>';

$names = array(
    'polar bear',
    'Grevy\'s zebra',
    'wet \'n wild!',
    'Nature: Alaska',
    '#myName',
    'dog % cat',
);
echo "names:\n";
print_r($names);

// "urlencode"ing them!
$names_encoded = array();
foreach($names as $name)
    $names_encoded[] = urlencode($name);

echo "\n\nnames_encoded:\n";
print_r($names_encoded);

$out = array();
foreach($names_encoded as $name_encoded)
    $out[] = preg_replace(
        array('#%[0-9a-fA-F]{2}#', '#\+#'), // finds the patterns like '%HH' (H represents an Hexadecimal digit), and '+'; the latter representing the spaces
        array('', '-'), // replace the '%HH' matches with nothing ('') and the '+' matches with '-'
        $name_encoded
    );

echo "\n\nout:\n";
print_r($out);
?>

输出的摘录将是:

out:
Array
(
    [0] => polar-bear
    [1] => Grevys-zebra
    [2] => wet-n-wild
    [3] => Nature-Alaska
    [4] => myName
    [5] => dog--cat
)

答案 2 :(得分:1)

如果要用连字符替换撇号和其他标点符号,可以使用这三行代码来创建干净的URL slugs。我已经使用了类似的片段多年,它相当坚固。需要注意的是,第三条线用于英式slu ;;如果需要,您需要将任何非英文字符添加到可接受的字符列表中。

.exe