开发者

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. But don't want to write out all the a href tags manually as I need to do a c开发者_如何学JAVAouple 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>";


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. (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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜