出于排序目的,我想忽略正在排序的标题中的文章。我已经能够在MySQL中完成它,但我需要在PHP中复制。
标题
SELECT Title,
CASE WHEN SUBSTRING_INDEX(Title, ' ', 1) IN ('a', 'an', 'the')
THEN CONCAT(
SUBSTRING(Title, INSTR(Title, ' ') + 1),
', ',
SUBSTRING_INDEX(Title, ' ', 1)
)
ELSE Title
END AS Sortable
FROM Titles
ORDER BY Sortable
我有一个复杂的数组处理例程,如果可能的话,我想用简单的preg_xxx语句替换它。已经足够修饰,相信我,“我试过的东西”只会混淆这个问题,哈哈。
为了将来参考,纯MySQL中的情况与此相同:
img{
max-width: 100%;
height: auto;
}
.overlay-box {
position: relative;
}
.overlay {
display: none;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(38,150,198,0.5);
}
.overlay:before {
content: '';
display: inline-block;
width: 1px;
height: 100%;
vertical-align: middle;
border: 1px solid;
}
.overlay p {
color: #ffffff;
text-align: center;
display: inline-block;
vertical-align: middle;
width: 90%;
}
.overlay-box:hover .overlay {
display: block;
}
答案 0 :(得分:8)
从字符串的开头捕获并反向替换反向引用:
$sortable = preg_replace('/^(A|An|The) (.+)/', '$2, $1', $title);
^
匹配字符串的开头.
1次或多次+
()
是一个捕获组,可捕获匹配的内容,并在第一组的$1
和第二组的$2
替换中引用答案 1 :(得分:0)
我想你可以针对一系列文章检查字符串,将其爆炸,然后将文章移到最后:
$articles = ['A', 'An', 'The'];
$titles = [
'A Tale of Two Cities',
'An Unexpected Journey',
'The Lord of the Rings',
];
foreach ($titles as $title) {
if (preg_match('/^('.implode('|', $articles).')\s/', $title, $matches)) {
$title = preg_replace('/^'.$matches[1].'\s/', '', $title).', '.$matches[0];
}
}