Wrapping text in  tags I needed a script to wrap phrases with  tag, they all are separated by commas, for i.e "Alan, Bob, John".

I came to this result:

        $actors = explode(',', $item-&gt;casting);

        $item-&gt;casting = array_map(function($a) {
            $a = trim($a);
            return '&lt;a href="'.$a.'"&gt;'.$a.'&lt;/a&gt;';
        }, $actors);

        echo implode(' ', $item-&gt;casting);


I'd like to know is there a better way achieve this result ?
Your code doesn't handle names with spaces within, nor names with characters that are illegal for URL's. To accomodate these two issues, you could use something like:

$item-&gt;casting = 'André, Ann Charlotte, Bob, Kåre Ronny';

$actors = array();

foreach (explode(',', $item-&gt;casting) as $actor) {
    $actor = trim($actor);
    $actors[] = '&lt;a href="'. urlencode($actor) .'"&gt;'. $actor .'&lt;/a&gt;';
}

echo implode(', \n', $actors);


Which would produce the following output:

&lt;a href="Andr%C3%A9"&gt;André&lt;/a&gt;, 
&lt;a href="Ann+Charlotte"&gt;Ann Charlotte&lt;/a&gt;, 
&lt;a href="Bob"&gt;Bob&lt;/a&gt;, 
&lt;a href="K%C3%A5re+Ronny"&gt;Kåre Ronny&lt;/a&gt;


I kind of prefer building the $actors array, as that allows me easily to change output variants. But you could also opt for using echo directly, which would eliminate the need for the entire $actors table. I did however removing the original $actors and included that directly in the foreach, as I try to keep intermediary variables to a minimum.

PS! Depending on font settings and encoding elsewhere, you could consider calling htmlspecialchars() in addition before using in the URL.