php 将链接包裹在单词周围
你好,我想在这个代码块中的给定月份将谷歌搜索链接标签包裹在每个要种植的食物周围。但不想手动写出所有 a 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>";
Hello I would to wrap google search link tags round each of the foods to plant in a given month in this code block. But don't want to write out all the a href tags manually as I need to do a couple of blocks similar to this and it will be quite time consuming. Is there a way to get php to do this using something like 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;
}?>
For example for december, case 12 I would like the line to be:
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>";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果只是没有标点符号的单词,您可以使用一些正则表达式魔法:
If it's only words without punctuation, you can use a bit of regex magic:
这是使用数组的另一种解决方案:
Here is another solution using arrays:
首先,更改 echo 语句以将它们存储在变量中。 (并丢失空格)就像这样:
IDE 中的查找替换应该可以快速解决这个问题。然后在 switch 块之后执行以下操作:
这实际上会留下一个训练逗号和空格。因此,您可能希望首先将字符串连接到新变量中,修剪尾随空格,然后回显它。但这将帮助您开始。
First, change the echo statements to store them in a variable. (and lose the spaces) like so:
A find-replace in your IDE should quickly sort that out. Then do the following after the switch block:
That will actually leave a training comma and space. So, you may want to concat the string in a new variable first, trim the trailing space, then echo it. But this will get you started.