你好我想在这个代码块中给每个食物包装谷歌搜索链接标签。但是不想手动写出所有的href标签,因为我需要做几个与此类似的块,这将非常耗时。有没有办法让php使用像preg_replace这样的东西来做到这一点。
<?php switch(date(n)) {
case 1:
echo "Garlic, Onion";
break;
case 2:
echo "Cabbage, Carrot, Garlic, Leek, Pea, Wheat";
break;
case 3:
echo "Cabbage, Carrot, Chives, Aubergine, Garlic, Leek, Lettuce, Pea, Rhubarb, Spinach, Tomato";
break;
case 4:
echo "Cabbage, Carrot, Chives, Cucumber, Aubergine, Garlic, Leek, Lettuce, Pea, Pumpkin, Rhubarb, Spinach, Tomato, courgette";
break;
case 5:
echo "Asparagus, Broad Beans, Cabbage, Carrot, Chives, Cucumber, Leek, Lettuce, Oregano, Pea, Pumpkin, Rhubarb, Spinach, Tomato, courgette";
break;
case 6:
echo "Asparagus, Broad Beans, Cabbage, Carrot, Cucumber, Kale, Lettuce, Oregano, Pea, Pumpkin, Rhubarb, Sage, Spinach, Tomato, courgette";
break;
case 7:
echo "Asparagus, Broad Beans, Broccoli, Brussel Sprouts, Cabbage, Carrot, Cauliflower, Cucumber, Kale, Oregano, Parsley, Rhubarb, Sage";
break;
case 8:
echo "Asparagus, Broccoli, Brussel Sprouts, Cabbage, Carrot, Cauliflower, Kale, Oregano, Parsley, Sage";
break;
case 9:
echo "Asparagus, Broccoli, Brussel Sprouts, Cabbage, Carrot, Cauliflower, Kale, Oregano, Parsley";
break;
case 10:
echo "Cabbage, Onion, Parsley";
break;
case 11:
echo "Apples, Garlic, Onion";
break;
case 12:
echo "Apples, Garlic, Onion";
break;
}?>
例如对于12月,案例12我希望该行:
echo "<a href='http://www.google.co.uk/search?q=Apples'>Apples</a>, <a href='http://www.google.co.uk/search?q=Garlic'>Garlic</a>, <a href='http://www.google.co.uk/search?q=Onion'>Onion</a>";
答案 0 :(得分:2)
如果只是没有标点符号的单词,你可以使用一些正则表达式魔术:
function renderGoogleLinks($line) {
return preg_replace('/([^[:punct:]\s\t\n\r]+)/', '<a href="http://www.google.co.uk/search?q=\\1">\\1</a>', $line);
}
echo renderGoogleLinks("Apples, Garlic, Onion");
答案 1 :(得分:1)
以下是使用数组的另一种解决方案:
<?php
$plants = array(
1 => array('Garlic', 'Onion'),
2 => array('Cabbage', 'Carrot', 'Garlic', 'Leek', 'Pea', 'Wheat'),
3 => array('Cabbage', 'Carrot', 'Chives', 'Aubergine', 'Garlic', 'Leek', 'Lettuce', 'Pea', 'Rhubarb', 'Spinach', 'Tomato'),
4 => array('Cabbage', 'Carrot', 'Chives', 'Cucumber', 'Aubergine', 'Garlic', 'Leek', 'Lettuce', 'Pea', 'Pumpkin', 'Rhubarb', 'Spinach', 'Tomato', 'courgette'),
5 => array('Asparagus', 'Broad Beans', 'Cabbage', 'Carrot', 'Chives', 'Cucumber', 'Leek', 'Lettuce', 'Oregano', 'Pea', 'Pumpkin', 'Rhubarb', 'Spinach', 'Tomato', 'courgette'),
6 => array('Asparagus', 'Broad Beans', 'Cabbage', 'Carrot', 'Cucumber', 'Kale', 'Lettuce', 'Oregano', 'Pea', 'Pumpkin', 'Rhubarb', 'Sage', 'Spinach', 'Tomato', 'courgette'),
7 => array('Asparagus', 'Broad Beans', 'Broccoli', 'Brussel Sprouts', 'Cabbage', 'Carrot', 'Cauliflower', 'Cucumber', 'Kale', 'Oregano', 'Parsley', 'Rhubarb', 'Sage'),
8 => array('Asparagus', 'Broccoli', 'Brussel Sprouts', 'Cabbage', 'Carrot', 'Cauliflower', 'Kale', 'Oregano', 'Parsley', 'Sage'),
9 => array('Asparagus', 'Broccoli', 'Brussel Sprouts', 'Cabbage', 'Carrot', 'Cauliflower', 'Kale', 'Oregano', 'Parsley'),
10 => array('Cabbage', 'Onion', 'Parsley'),
11 => array('Apples', 'Garlic', 'Onion'),
12 => array('Apples', 'Garlic', 'Onion'),
);
function googleLink($myarray) {
$str = '';
foreach($myarray as $var) {
$end = (next($myarray) == true) ? ', ' : '.';
$str .= '<a href="http://www.google.co.uk/search?q='.$var.'">'.$var.'</a>'.$end;
}
return $str;
}
echo googleLink($plants[date(n)]);
?>
答案 2 :(得分:0)
首先,更改echo语句以将它们存储在变量中。 (并丢失空格)如下:
$food = "Apples,Garlic,Onion";
IDE中的find-replace应该快速排序。然后在切换块之后执行以下操作:
$foods = explode(',', $food);
foreach ($foods as $food) {
echo '<a href="http://www.google.co.uk/search?q=';
echo $food;
echo '">' . $food . '</a>, ';
}
这实际上会留下训练逗号和空格。因此,您可能希望首先在新变量中连接字符串,修剪尾随空格,然后回显它。但这会让你开始。
答案 3 :(得分:0)
function renderGoogleLink($searchTerm){
$anchor_template = '<a href="http://www.google.co.uk/search?q=#TERM#">#TERM#</a>';
echo preg_replace("#TERM#", $searchTerm, $anchor_template);
}