PHP Functional Programming - A code snippet

Given a string of comma-separated values, how do you convert each of them into a link of the form: --item-- and return a comma-separated list of these strings? Snippet 1: Snippet 2 (uses functional...

1 min read
Historical Content: This post was published more than 3 years ago and has been preserved for historical reference. Some information may be outdated, and it reflects my understanding of the technology at the time of writing.

Given a string of comma-separated values, how do you convert each of them into a link of the form: —item— and return a comma-separated list of these strings?

Snippet 1:

<?php
//Make these links to google.com

$string = "news,reader,mail";

$array_of_string = split(",",$string);

$final = array();

foreach($array_of_string as $item){
	$final[] = "<a href='http://".$item.".google.com/'>$item</a>";
}

echo implode(", ",$final);
?>

Snippet 2 (uses functional constructs):

<?php

//Make these links to google.com

$string = "news,reader,mail";

$array_of_string = split(",",$string);

echo implode(", ",
		array_map(
			create_function('$item',
	'return "<a href=\"http://".$item.".google.com/\">".$item."</a>";'
				),$array_of_string
			)
		);

?>