-1

I array text from custom field "black, grey, white" with this code:

<?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'colors', true); ?>

I want show me like this:

black</br>
grey</br>
white

It's possible with PHP? Many thanks

2
  • str_replace(",", ",<br>", $string);
    – Pekka
    Sep 29, 2013 at 1:34
  • 3
    For the record, it's <br />, not </br>.
    – Steven V
    Sep 29, 2013 at 1:34

3 Answers 3

1

Use str_replace():

echo str_replace(",", "<br />", get_post_meta($postid, 'colors', true));
3
  • But how put <?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'colors', true); ?> With that¿
    – vektor
    Sep 29, 2013 at 2:34
  • I don't know how use this code or where. sorry.i'm designer and i'm learning. Can you help me?
    – vektor
    Sep 29, 2013 at 13:22
  • @vektor, Sorry I was out. Do you still need help?
    – Starx
    Sep 30, 2013 at 2:01
0

If you want to add <br> after a "," you can do this:

$text = preg_replace("/,/", "<br>", get_post_meta($postid, 'colors', true));
3
  • 4
    Why regex for a simple string replacement? It would work, but str_replace() would suffice.
    – Wiseguy
    Sep 29, 2013 at 1:36
  • You are right but I always use regex thats why I use preg_replace. Sep 29, 2013 at 1:39
  • 1
    Ok, though that raises another question: why do you always use regex? :-p
    – Wiseguy
    Sep 29, 2013 at 1:40
0
str_replace(", "    ,    ", <br  />"   ,  $string) ;

The format is ("from", "to" , "what")

Not the answer you're looking for? Browse other questions tagged or ask your own question.