I have a variable $keywords
开发者_StackOverflowThe content of this variable are words separated by commas or spaces for example:
$keywords= key1,key2,key3
Or
$keywords=key1 key2 key3
the table that I have is:
<table width="500" border="1">
<tr>
<td height='auto'>Keywords: $keywords</td>
</tr>
</table>
I want explode $keywords
in key1 key2 key3 ...
And associate to each separated word a predifined URL:
http://miosite.com/search/label/key1
http://miosite.com/search/label/key2
http://miosite.com/search/label/key3
So I want get this:
<table width="500" border="1">
<tr>
<td height='auto'>Keywords: key1,key2,key3</td>
</tr>
</table>
Where
key1=http://miosite.com/search/label/key1
key2=http://miosite.com/search/label/key2
key1=http://miosite.com/search/label/key3
How to?
You could check for the comma to determine which delimiter to explode on, then put together the string again:
if (strpos($keywords,",") !== FALSE) {
$keys = explode(",",$keywords);
} else {
$keys = explode(" ",$keywords);
}
$keywords = "";
foreach ($keys as $key) $keywords .= "http://miosite.com/search/" . $key . "<BR>";
EDIT: Apparently the object is to REMOVE the site address, not add it... no one figured that out. new code:
if (strpos($keywords,",") !== FALSE) {
$keys = explode(",",$keywords);
} else {
$keys = explode(" ",$keywords);
}
$keywords = "";
foreach ($keys as $key) $keywords .= str_ireplace("http://miosite.com/search/","",$key) . ",";
$keywords = substr($keywords,0,strlen($keywords)-1);
Hackish, but give that a shot.
Edit: Oh now they need to be linked? LOL
if (strpos($keywords,",") !== FALSE) {
$keys = explode(",",$keywords);
} else {
$keys = explode(" ",$keywords);
}
$keywords = "";
foreach ($keys as $key) {
$newkey = str_ireplace("http://miosite.com/search/","",$key);
$keywords .= "<a href=\"" . $key . "\">" . $newkey . "</a>,";
}
$keywords = substr($keywords,0,strlen($keywords)-1);
Ok, try that out.
$keywords = explode(',',$keywords);
foreach($keywords as $key) echo("<a href=\"http://miosite.com/search/{$key}\">{$key}</a>");
Keywords:
$keywords = explode(',',$keywords);
$tmpArray = array();
foreach($keywords as $key) {
$tmpArray[] = 'http://miosite.com/search/label/'.$key;
}
echo implode(",", $tmpArray);
Iam just rewrting by embeddg php within html & taken are of caommas between your links:
<?php
$keywords = 'key1 key2 key3';
$keywords = preg_split('/[, ]/',$keywords);
foreach($keywords as $key)
$links[]= "<a href=http://miosite.com/search/{$key}\>{$key}</a>";
print_r($links);
$keywords = implode(',',$links);
?>
<table width="500" border="1">
<tr>
<td height='auto'>Keywords: <?php echo $keywords ;?>
</td>
</tr>
</table>
精彩评论