Support

Account

Home Forums Front-end Issues PHP, explode() and field()

Solved

PHP, explode() and field()

  • Hi,

    Well, I’m attempting to solve a problem meself, but unfortunally, it doesn’t work!

    I entered this string in field (ACF, back-end)
    Field : ABC|DEF|GHI|JKL|MNO

    I’d like to get this result in my page:
    ABC / DEF / GHI / JKL / MNO (separated by spaces between slashes, but no slash at the end of string)

    I’ve found something, but it seems bugged :

    $str = get_field(""MyField);
    $str = split("|",trim($str));
    
    for ($i = 0; $i < count($str); $i++) {
        if( eregi("".$str[$i]." / ",$str) ) { echo $str[$i]; }
    }

    By advance, thank you for your precious help!

    Cheers,

    Nico.

  • Assuming that you have the complete string Field : ABC|DEF|GHI|JKL|MNO:

    
    $data = explode( ':', get_field('MyField' );
    $parts = explode( '|', $data[1] );
    foreach($parts as $k=>$part){
        if($k && ($k < count($parts) ) ) echo ' / ';
        echo $part;
    }
    

    … but if your field string is only ABC|DEF|GHI|JKL|MNO, you can leave out the first explode step:

    
    $parts = explode( '|', get_field('MyField' );
    foreach($parts as $k=>$part){
        if($k && ($k < count($parts) ) ) echo ' / ';
        echo $part;
    }
    
  • Thank you very much! it rolls!

  • Haha right on. Cheers!

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

The topic ‘PHP, explode() and field()’ is closed to new replies.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.