我有一个数据库表,其中包含一个名为URL的字段,显示如下所示的值:Spain / Flower,Japan / Flag。因此,URL可能看起来像MySite / World / Spain / Bird
我想将该项目与其他几个项目合并,并将它们合并到一个新网站中。捕获的是URL必须从Spain / Flower更改为Spain_Flower。换句话说,我想用下划线替换所有正斜杠。
在下面的查询中,$ MyURL(:MyURL)将等于页面网址,例如西班牙/鸟。任何人都可以建议一种方法让它接受Spain_URL作为正确的URL吗?因此,如果我输入URL MySiteNew / Spain_Bird,它应该显示,但MySiteNew / Spain / Bird应该会收到404错误消息。
(我正在使用PHP / MySQL。)
$sql = "SELECT SUM(num) as num FROM (
SELECT COUNT(URL) AS num FROM gs WHERE URL = :MyURL
UNION ALL
SELECT COUNT(URL) AS num FROM people WHERE URL = :MyURL
) AS X";
答案 0 :(得分:2)
概括:您正在寻找名为“Inflection”的功能,即在不同格式之间翻译单词或短语:
Inflect::camelCase('A nice string'); // ANiceString
Inflect::underscore('A nice string'); // a_nice_string
Inflect::humanize('a_nice_string'); // A Nice String
在各种php框架中都有这种类型的库:http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html
您可以根据您的具体需求调整/扩展此类内容:
Inflect::makeURL('MySite/Spain/Bird'); // MySite/Spain_Bird
class Inflect {
public static function makeURL($string) {
// This won't do exactly what you want but you get the idea
return str_replace('/', '_', $string);
}
}
答案 1 :(得分:0)
愚蠢的我 - 我知道有一个简单的解决方案,但直到现在我才得到它......
$String = str_replace('_', '/', $MyURL);
$sql = "SELECT SUM(num) as num FROM (
SELECT COUNT(Taxon) AS num FROM gz_life WHERE Taxon = :MyURL AND G1 = 1
UNION ALL
SELECT COUNT(Name) AS num FROM gw_geog WHERE Name = :MyURL AND G1 = 1
UNION ALL
SELECT COUNT(URL) AS num FROM gs WHERE URL = :String AND G1 = 1