php 将链接包裹在单词周围

发布于 2024-10-04 14:51:43 字数 1919 浏览 4 评论 0原文

你好,我想在这个代码块中的给定月份将谷歌搜索链接标签包裹在每个要种植的食物周围。但不想手动写出所有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

戴着白色围巾的女孩 2024-10-11 14:51:43

如果只是没有标点符号的单词,您可以使用一些正则表达式魔法:

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");

If it's only words without punctuation, you can use a bit of regex magic:

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");
治碍 2024-10-11 14:51:43

这是使用数组的另一种解决方案:

<?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)]);

?>

Here is another solution using arrays:

<?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)]);

?>
红玫瑰 2024-10-11 14:51:43

首先,更改 echo 语句以将它们存储在变量中。 (并丢失空格)就像这样:

$food = "Apples,Garlic,Onion";

IDE 中的查找替换应该可以快速解决这个问题。然后在 switch 块之后执行以下操作:

$foods = explode(',', $food);
foreach ($foods as $food) {
    echo '<a href="http://www.google.co.uk/search?q=';
    echo $food;
    echo '">' . $food . '</a>, ';
}

这实际上会留下一个训练逗号和空格。因此,您可能希望首先将字符串连接到新变量中,修剪尾随空格,然后回显它。但这将帮助您开始。

First, change the echo statements to store them in a variable. (and lose the spaces) like so:

$food = "Apples,Garlic,Onion";

A find-replace in your IDE should quickly sort that out. Then do the following after the switch block:

$foods = explode(',', $food);
foreach ($foods as $food) {
    echo '<a href="http://www.google.co.uk/search?q=';
    echo $food;
    echo '">' . $food . '</a>, ';
}

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.

离不开的别离 2024-10-11 14:51:43
function renderGoogleLink($searchTerm){
     $anchor_template = '<a href="http://www.google.co.uk/search?q=#TERM#">#TERM#</a>';
     echo preg_replace("#TERM#", $searchTerm, $anchor_template);
}
function renderGoogleLink($searchTerm){
     $anchor_template = '<a href="http://www.google.co.uk/search?q=#TERM#">#TERM#</a>';
     echo preg_replace("#TERM#", $searchTerm, $anchor_template);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文