Andy
Andy

Reputation: 3021

Find - using PHP and wrap everything after in <span>

I am printing using PHP

<?php echo $article->link($article->title()); ?>

This string holds something like

"Team Member - Job Title"

What id like to do is wrap everything after the dash in a span so i can change its colour.

Any help would be greatfully appreciated.

Upvotes: 0

Views: 165

Answers (3)

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124828

Try this:

<?php
list($a, $b) = explode(' - ', $article->title());
echo $article->link($a.' - <span>'.$b.'</span>');
?>

Upvotes: 2

Tammo
Tammo

Reputation: 1178

Not elegant, but fast:

$str = 'Team Member - Job Title' . '</span>';
$str = strtr(
    $str,
    '-',
    '- <span>'
);

Upvotes: 0

SoapBox
SoapBox

Reputation: 20609

preg_replace is probably the easiest way to do this:

preg_replace('/([^-]*\s*-\s*)(.*)/', '\\1<span class="whatever">\\2</span>', $article->link($article->title()) )

Upvotes: 0

Related Questions