I am trying to write a piece of code that:
- converts a string containing a list of people,
$currentAuthors
, into an array$authors
with each person being a value in that array. - compares each person in that array with each person in another array, and if they match, wraps the value in
$authors
inside<a href="#">
and</a>
(for examples sake). - create another string from this array, using commas ", " and " and " separators so that the string reads well.
Basically, it makes certain people's names into links.
Code is here and this works:
$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;
Result is:
Initial string: Paul Peters, Joe Bloggs, Chloe Brown, Sarah Smith, Anna Smith and Mark Jones
Final string: Paul Peters, Joe Bloggs, Chloe Brown, Sarah Smith, Anna Smith and Mark Jones
However, if I make this code into a function the links for Joe Bloggs and Anna Smith are no longer added. Any ideas why? Must be something to with the values in $authors
not being changed properly.
Code not working here:
$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);
Result is without links added this time:
Initial string: Paul Peters, Joe Bloggs, Chloe Brown, Sarah Smith, Anna Smith and Mark Jones
Final string: Paul Peters, Joe Bloggs, Chloe Brown, Sarah Smith, Anna Smith and Mark Jones
Thanks very much! I'm new to learning PHP.