1

The variable looks like this:

$keywords = 'key1, key2, key3, key4';

How can i create links like:

<a href="../tag/key1">key1</a>, <a href="../tag/key2">key2</a>, <a href="../tag/key3">key3</a>, <a href="../tag/key4">key4</a>

using php?


// Edited

What if $is defined with

$keywords = 'key1, keyword two, key3, key four';

or even other order?

<a href="../tag/key1">key1</a>, <a href="../tag/keyword+two">keyword two</a>, <a href="../tag/key3">key3</a>, <a href="../tag/key+four">key four</a>

i mean, if one of the 'keyword' it is formed from two words add a '+' sign between them at the link

How can this been done?

1
  • @Balanivash -- looks like a string to me Commented Jun 28, 2011 at 19:40

6 Answers 6

6

Try this out:

foreach(explode(', ',$keywords) as $value){
    echo "<a href='../tag/$value'>$value</a>";
}

To do with commas:

$keyArray = explode(', ',$keywords);
$keyLength = count($keyArray);
foreach($keyArray as $key=>$value){
    echo "<a href='../tag/$value'>$value</a>";
    if($key < ($keyLength - 1)){
          echo ", ";
    }
}

UPDATE TO OP:

$keyArray = explode(', ',$keywords);
$keyLength = count($keyArray);
foreach($keyArray as $key=>$value){
    echo "<a href='../tag/";
    $vArr = explode(' ', $value);
    $vLength = count($vArr);
    foreach($vArr as $kv=>$v){
         echo $v;
         if($kv < ($vLength - 1))  echo "+";
    }
    echo "'>$value</a>";
    if($key < ($keyLength - 1)){
          echo ", ";
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

I think you beat me by a second! Apart from saving a line are their any benefits to doing your explode inside the foreach?
@TimCooper i added the commas although im not sure why :-P
@Toby ur way might be better (in my way it has to get the index of the explode every time)
@Neal: That adds an unnecessary comma to the end of the list.
I am going to run some tests.
|
3

The first thing you want to do is explode that string.

$links = explode(',', $keywords);

Then you want to loop it...

for($counter = 0; $counter < sizeof($links); $counter++) {
    if($counter > 0) {
        echo ", ";
    }
    echo "<a href='../tag/".str_replace(' ', '+', trim($link[$counter]))."'>{$link[$counter]}</a>";
}

5 Comments

@Toby, now it definitely does
Nope, so the first time around the loop no comma, then every other time until the end of the loop the <a> will be prefaced with a comma.
thank you, but it has a problem: <a href='../tag/funny'>funny</a>, <a href='../tag/+video+funny'> video funny</a>
it has a space in in the from the link text and a + in the front of the tag name...
I have now trimmed the text first. Please update your question to include this information.
2

The following is updated to your new requirements:

function keyword_url($arr)
{
    return sprintf('%s<a href="../tag/%s">%s</a>', $arr[1], urlencode($arr[2]), $arr[2]);
}

$keywords = 'key1, key2 piece, key3, key4';
echo preg_replace_callback('/((?:^)|(?:, *))([\w ]+)/', 'keyword_url', $keywords);

1 Comment

Does this solve the leading space issue if the keyword is ' funny pictures'?
1
 <?php  $keyword = explode(',',$keywords); ?> 
 <a href="../tag/<?php echo $keyword[0]; ?>">key1</a>
 <a href="../tag/<?php echo $keyword[2]; ?>">key3</a>

Comments

1

First use explode() to split your keyword string into an array

From there there are a number of ways to turn an array into a series or array of strings. You could even use implode to bring an array of link strings back into a single string.

Comments

1
<?php

$keywords = 'key1, key2, key3, key4';

$vars = explode(',',$keywords);
foreach($vars as $key => $value)
{
    echo '<a href="'.$value.'" title="' . $value . '">'.$value.'</a><br/>';
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.