Home Forums General Issues String replace in a value


String replace in a value

  • I have some checkbox options which return values like this:
    address1, address2, postcode
    I’d like to remove the commas in the returned values and replace with a line break.
    This is where I’ve got to, alas it doesn’t work.
    <?php echo str_replace(', ', '<br />', get_field('myfield', 'option')); ?>

  • get_field() for a checkbox field returns an array of values, not a string. the_field() outputs a comma separated sting because it is attempting to stringify the array.

    echo implode('<br />', get_field('my_field'));
  • Thanks John for the reply, however I don’t think I was very clear in my question.
    This is how the whole checkbox selection is set up…

    address1, address2, postcode3 : selectA
    address4, address5, postcode6 : selectB
    address7, address8, postcode9 : selectC

    so the if I check selectA the whole value I get back is “address1, address2, postcode3” rather than an array (I think).
    That’s the part I’d like to remove the commas and replace with a line break.

  • The checkbox field is still going to return an array.

    $values = get_field('checkbox');
    if ($values) {
      foreach ($values as $value) {
        echo str_replace(', ', '<br />', $value);
  • Perfect. Thank you.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘String replace in a value’ is closed to new replies.