将链接环绕在单词php上
您好,我想在这个代码块中,在给定的一个月内,在每个要种植的食物周围包装谷歌搜索链接标签。但是我不想手动写出所有的a href标记,因为我需要做几个类似的块,这将非常耗时。有没有一种方法可以让php使用preg_replace之类的东西来实现这一点将链接环绕在单词php上,php,preg-replace,hyperlink,Php,Preg Replace,Hyperlink,您好,我想在这个代码块中,在给定的一个月内,在每个要种植的食物周围包装谷歌搜索链接标签。但是我不想手动写出所有的a href标记,因为我需要做几个类似的块,这将非常耗时。有没有一种方法可以让php使用preg_replace之类的东西来实现这一点 <?php switch(date(n)) { case 1: echo "Garlic, Onion"; break; case 2: echo "Cabbage,
<?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>";
echo“,”;
首先,更改echo语句以将其存储在变量中。(并失去空间)就像这样:
IDE中的查找替换应该可以很快解决这个问题。然后在开关块后执行以下操作:
$foods = explode(',', $food);
foreach ($foods as $food) {
echo '<a href="http://www.google.co.uk/search?q=';
echo $food;
echo '">' . $food . '</a>, ';
}
$foods=爆炸(',',$food);
foreach($foods as$food){
回声',';
}
这实际上会留下一个训练逗号和空间。因此,您可能需要首先在新变量中压缩字符串,修剪尾随空间,然后回显它。但这将让您开始。函数rendergoogellink($searchTerm){
function renderGoogleLink($searchTerm){
$anchor_template = '<a href="http://www.google.co.uk/search?q=#TERM#">#TERM#</a>';
echo preg_replace("#TERM#", $searchTerm, $anchor_template);
}
$anchor_模板=“”;
echo preg_replace(“#TERM#”,$searchTerm,$anchor_模板);
}
如果只是没有标点符号的单词,您可以使用一点正则表达式的魔力:
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");
函数renderGoogleLinks($line){
返回preg_replace('/([^[:punct:][\s\t\n\r]+)/',''$line);
}
echo renderGoogleLinks(“苹果、大蒜、洋葱”);
下面是另一个使用阵列的解决方案:
<?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)]);
?>
啊,我没有处理食物清单。下面的答案很好地说明了这一点。用调用上面的my RenderGogleLink函数替换他的三行回声。
<?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)]);
?>