Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP – Make PHP function output multiple instances

I have $tutorial->difficulty which sometimes has multiple outputs, and for each output that goes into the breakCommas function, I need to output a link for each one. Currently with how I have it, when $tutorial->difficulty outputs multiple items, it basically clumps them all into a single link. From what I’ve read I realize you can’t have multiple outputs per function. Any ideas how I can separate it to where it outputs multiple links?

PHP

function breakCommas($str) {
  return ('<a href="'.str_replace(' ','-',strtolower($str)).'-tutorial">'.$str.'</a>');
}

HTML

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div>
  <?=breakCommas($tutorial->difficulty)?>
</div> 

>Solution :

try this function:

function breakCommas($str) {
    $s = explode (",", $str);
    $a = array();
    foreach ($s as $st) {
        $a[] = "<a href=\"".str_replace(" ","-",strtolower($st))."-tutorial\">{$st}</a>";
    }
    return implode("", $a);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading