0

I have PHP variable like:

$biling_cycles = 'Monthly,Annually';

I want it to be:

$biling_cycles = '<span> Monthly, </span> <span> Annually </span>';
0

4 Answers 4

1

You can do it as follows:

$billing_cycles = 'Monthly, Annually';
$temp = '';
foreach (explode(', ', $billing_cycles) as $key => $value) {
    $temp .= "<span> $value </span>,";
}
$biling_cycles = rtrim($temp, ','); // removes trainling comma

echo $billing_cycles;

Result is: <span> Monthly </span>,<span> Annually </span>

Sign up to request clarification or add additional context in comments.

Comments

0

Follow these simple steps:

$biling_cycles = 'Monthly, Annually';
$cycles = explode(', ', $biling_cycles);
$span_added = "";
foreach($cycles as $cycle){
$span_added = $span_added+"<span>"+$cycle+"</span>, ";
}
echo $span_added;

Output:

<span> Monthly </span>,<span> Annually </span>

Comments

0

The code Below Works for me:

<?php
 $biling_cycles = 'Monthly, Annually';
 $cycles = explode(', ', $biling_cycles);
 foreach($cycles as $cycle):
  echo '<span>';
  echo $cycle;
  echo "</span>";
 endforeach;
?>

Comments

0

It is not necessary to explode and iterate.

Just replace all commas with a comma, space, closing tag, space, opening tag, space. Then wrap the whole thing in an openong and closing tag.

Code: (Demo)

$billing_cycles = 'Monthly,Annually';

echo '<span> '
     . str_replace(',', ', </span> <span> ', $billing_cycles)
     . ' </span>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.