简体   繁体   English

包装链接圆词PHP

[英]Wrap links round words php

Hello I would to wrap google search link tags round each of the foods to plant in a given month in this code block. 您好,我想在此代码块中将google搜索链接标签环绕在给定月份中要种植的每种食物。 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. 但是不想手动写出所有的href标记,因为我需要做几个与此类似的块,这将非常耗时。 Is there a way to get php to do this using something like preg_replace. 有没有办法让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;
    }?>

For example for december, case 12 I would like the line to be: 例如,对于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>";

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

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

?>

First, change the echo statements to store them in a variable. 首先,更改echo语句以将它们存储在变量中。 (and lose the spaces) like so: (并丢失空格),如下所示:

$food = "Apples,Garlic,Onion";

A find-replace in your IDE should quickly sort that out. IDE中的查找替换应该可以快速解决。 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. 但是,这将让你开始。

function renderGoogleLink($searchTerm){
     $anchor_template = '<a href="http://www.google.co.uk/search?q=#TERM#">#TERM#</a>';
     echo preg_replace("#TERM#", $searchTerm, $anchor_template);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM