Support

Account

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

Solved

PHP, explode() and field()

    • Nico

    • July 12, 2013 at 10:30 pm

    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;
    }
    
    • Nico

    • July 13, 2013 at 7:10 am

    Thank you very much! it rolls!

  • Haha right on. Cheers!

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

You must be logged in to reply to this topic.

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.