I have an array $x = array(a,b,c); Each element of the array is a url.
There is a function link($x[$i]) which converts every element of the array into a hyperlink.
function convert($x){
$new = explode(',',$x);
for($i=0;$i<count($new);$i++){
echo link($new[$i]); // converts the url to hyperlink
}
This displays the output as url1 url2 etc. But wht if i want the comma ',' between the 2 urls. How do i get that, without the comma ',' being part of the hyperlink?
desired output would be
hyperlink1 , hyperlink2 etc // note comma ',' should not be part of the hyperlink
You can use the array_map and implode functions to make this easy.
$urls = array('http://www.google.com/', 'http://www.stackoverflow.com/');
$links = array_map("link", $urls);
echo implode(', ', $links);
function convert($x)
{
$string = '';
foreach($x as $element)
{
$string .= link($element).',';
}
$string = rtrim($string, ',');
echo $string;
}
This to avoid the last link to have a comma at the end, as you want. // echoes link1,link2,link3
$x = array(
'http://google.pl',
'http://yahoo.com',
'http://msn.com'
);
function convert($array){
$res = array();
foreach ( $array as $url ) {
$res[] = '<a href="' . $url . '">' . $url . '</a>';
}
return implode(', ', $res);
}
echo convert($x);
function convert($x){
for($i=0;$i<count($new);$i++){
echo link($new[$i]) . ($i<count($new)-1?", ":""); // converts the url to hyperlink
}
$x = array('foo', 'bar', 'baz');
// then either:
$x = array_map('link', $x);
// or:
foreach ($x as &$value) {
$value = link($value);
}
echo join(', ', $x);
$output = '';
foreach (explode(',',$x) as $url)
$output .= link($url);
$output = substr($output,0,-1);
echo $output;
function convert($x){
return implode(',', array_map('link', explode(',',$x)));
}