【发布时间】:2013-11-21 16:34:28
【问题描述】:
我正在尝试编写一段代码:
- 转换一个包含人员列表的字符串,
$currentAuthors, 放入数组$authors,每个人都是其中的一个值 数组。 - 将该数组中的每个人与另一个数组中的每个人进行比较,如果匹配,则将
$authors中的值包装在<a href="#">中并</a>(例如)。 - 从此数组创建另一个字符串,使用逗号 "、" 和 " 和 " 分隔符,以便字符串读取良好。
基本上,它将某些人的名字变成链接。
代码在这里,这有效:
$currentAuthors = "Paul Peters, Joe Bloggs, Chloe Brown, Sarah Smith, Anna Smith and Mark Jones";
$linkableAuthors = array("Joe Bloggs","Anna Smith");
echo "Initial string: ".$currentAuthors."<br />";
// replace all instances of "and" with "," so all authors in $newAauthors are separated by a comma
$replaceAnds = str_replace(" and ", ",", $currentAuthors);
//create array from $currentAuthors, using comma as delimiter
$authors = explode(",", $replaceAnds );
$authorsCount = count($authors);
foreach ($authors as $key=>&$author) {
// Trim spaces from beginning and end of each author, if there are any
$author = trim($author);
foreach ($linkableAuthors as $linkableAuthor) {
// check if each value in $authors matches any of the $linkableAuthors, if so, make it a link.
if ($author == $linkableAuthor) {
$author = "<a href='#'>".$author."</a>";
}
}
}
$fullAuthorList = "";
// logic for recreating initial string with new links added in; creating separators
foreach ($authors as $key=>$authorFinal) {
$fullAuthorList .= $authorFinal;
if ($authorsCount==1) {
// do nothing, only one author
}
elseif ($key==$authorsCount-1) {
// do nothing, on last author
}
elseif ($key==$authorsCount-2) {
$fullAuthorList .= " and ";
}
else {
$fullAuthorList .= ", ";
}
}
$fullAuthorList .= "</p>";
echo "Final string: ".$fullAuthorList;
结果是:
初始字符串:Paul Peters、Joe Bloggs、Chloe Brown、Sarah Smith、Anna Smith 和 Mark Jones
最后一个字符串:Paul Peters、Joe Bloggs、Chloe Brown、Sarah Smith、Anna Smith 和 Mark Jones
但是,如果我将此代码变成一个函数,则不再添加 Joe Bloggs 和 Anna Smith 的链接。任何想法为什么?必须与 $authors 中的值未正确更改有关。
代码在这里不起作用:
$currentAuthors = "Paul Peters, Joe Bloggs, Chloe Brown, Sarah Smith, Anna Smith and Mark Jones";
$linkableAuthors = array("Joe Bloggs","Anna Smith");
function getAuthors ($input) {
echo "Initial string: ".$input."<br />";
// replace all instances of "and" with "," so all authors in $newAauthors are separated by a comma
$replaceAnds = str_replace(" and ", ",", $input);
//create array from $currentAuthors, using comma as delimiter
$authors = explode(",", $replaceAnds );
$authorsCount = count($authors);
foreach ($authors as $key=>&$author) {
// Trim spaces from beginning and end of each author, if there are any
$author = trim($author);
foreach ($linkableAuthors as $linkableAuthor) {
// check if each value in $authors matches any of the $linkableAuthors, if so, make it a link.
if ($author == $linkableAuthor) {
$author = "<a href='#'>".$author."</a>";
}
}
}
$fullAuthorList = "";
// logic for recreating initial string with new links added in; creating separators
foreach ($authors as $key=>$authorFinal) {
$fullAuthorList .= $authorFinal;
if ($authorsCount==1) {
// do nothing, only one author
}
elseif ($key==$authorsCount-1) {
// do nothing, on last author
}
elseif ($key==$authorsCount-2) {
$fullAuthorList .= " and ";
}
else {
$fullAuthorList .= ", ";
}
}
$fullAuthorList .= "</p>";
echo "Final string: ".$fullAuthorList;
}
getAuthors($currentAuthors);
结果这次没有添加链接:
初始字符串:Paul Peters、Joe Bloggs、Chloe Brown、Sarah Smith、Anna Smith 和 Mark Jones
最后的字符串:Paul Peters、Joe Bloggs、Chloe Brown、Sarah Smith、Anna Smith 和 Mark Jones
非常感谢!我是学习 PHP 的新手。
【问题讨论】:
标签: php arrays string if-statement foreach