PHP:获取变量中每个单词的当前完整 url 和 href


PHP: Getting current full url and href to each word in a variable

<?php
    $test = 'Ontario, Montreal, Quebec';
?>

我如何像这样使用单个变量但多个单词的页面当前完整 url 来单独对每个单词进行 href。

<a href="http://www.domain.com/canada">Ontario</a> <a href="http://www.domain.com/canada"> Montreal</a> <a href="http://www.domain.com/canada"> Quebec</a>

安大略省 蒙特利尔 魁北克省

这是 explode 的简单应用程序,它根据分隔符将字符串拆分为多个子字符串,在本例中为 ", "。

$test = "Ontario, Montreal, Quebec";
$words = explode(", ", $test);
foreach ($words as $word) {
    echo "<a href='http://www.domain.com/" . $_SERVER["REQUEST_URI"] . "'>$word</a>";
}

取自@jli,略有修改。

<?php
$test = "Ontario, Montreal, Quebec";
$words = explode(",", $test);
array_walk($words,"trim");
foreach ($words as $word) {
    echo "<a href='http://www.domain.com/{$word}'>$word</a>";
}
?>