How can i do this using php

Discussion in 'Programming' started by mumfry, Mar 21, 2013.

  1. #1
    Hello

    Gonna get straight to it

    I have an array
    and in it there are 5 items (this array was created from a single string using explode)
    What i do is
    I run a foreach statement like so
    foreach(explode(" ", $thestring) as $v){
      echo    $v. ' |';
      }
    Code (markup):
    what i would like to know is (no pipe symbol at the end)
    How can i display the pipe symbol(|) up to and just before the last item when i echo
    at the moment i get this
    item1 | item2 | item3 | item4 | item5 |<--- this is my problem that last 1

    what i want is this
    item1 | item2 | item3 | item4 | item5

    I know its simple but i dont want to perform a hack job on it or over do it with to much unnecessary code

    Any help with this would be greatly appreciated

    Thanks
     
    Solved! View solution.
    mumfry, Mar 21, 2013 IP
  2. xtmx

    xtmx Active Member

    Messages:
    359
    Likes Received:
    12
    Best Answers:
    4
    Trophy Points:
    88
    #2
    <?php
    $items = explode(" ", $thestring);
    echo implode(" | ", $items);
    PHP:
     
    xtmx, Mar 21, 2013 IP
  3. #3
    1. Just replace it:
    echo str_replace(' ', ' | ', $thestring);
    PHP:
    2. Use implode:
    foreach (explode(" ", $thestring) as $v) {
        $tmp_array[] = $v;
    }
     
    echo implode(' | ', $tmp_array);
    PHP:
     
    IGarret, Mar 21, 2013 IP
  4. webdeveloperindia

    webdeveloperindia Active Member

    Messages:
    90
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    73
    #4
    Yes IGarret is right his code will work
     
    webdeveloperindia, Mar 23, 2013 IP
  5. mumfry

    mumfry Active Member

    Messages:
    118
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    Thanks to all for your help... all answers was good
    but for my situation IGarret worked best
     
    mumfry, Mar 24, 2013 IP
  6. duckz

    duckz Active Member

    Messages:
    245
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    68
    #6
    do you need to run foreach out of explode?

    I thought explode is already returning an array?

    so

    <?php

    $tmp = explode(' ', $thestring);
    echo (implode(' | ', $tmp);

    ?>

    should be sufficient? no?
     
    duckz, Mar 28, 2013 IP
  7. mumfry

    mumfry Active Member

    Messages:
    118
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #7
    I see what you mean there Duckz
    But i went with the foreach statement because i would like to turn each value in the array into a link
     
    mumfry, Mar 28, 2013 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #8
    If that's a list of links separated by vertical breaks -- those vertical break characters probably don't belong in there and some more tags do... in which case I'd put those in a UL with each in a LI, and then border-left each of them adjusting the padding/margins as needed. (display:inline on the LI and inline-block on the anchor would make that easy). then just #menu li:first-child { border:0; }

    That way you have semantic markup of your list instead of using presentational markup -- which is basically what it sounds like you are doing.
     
    deathshadow, Mar 28, 2013 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    $itemList = explode(" ", $thestring);
    echo '
    	<ul id="someMenu">';
    foreach ($itemList as $item) {
    	echo '
    		<li><a href="',$item,'">',$item,'</a></li>';
    }
    echo '
    	</ul>';
    Code (markup):
    #someMenu {
    	list-style:none;
    }
    
    #someMenu li {
    	display:inline;
    }
    
    #someMenu li a {
    	display:inline-block;
    	padding:0 0.2em 0 0.6em; /* adjust as needed */
    	border-left:2px solid #000;
    }
    
    #someMenu li:first-child a {
    	padding-left:0;
    	border:0;
    }
    Code (markup):
    Again, semantic markup BEFORE you start thinking about what it's going to look like! Of course, IE6 won't have the border erased on the first element, but really who gives a rats ass about that in IE6 at this point.
     
    deathshadow, Mar 28, 2013 IP
  10. mumfry

    mumfry Active Member

    Messages:
    118
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #10
    Ok
    Will give that one a try
     
    mumfry, Mar 29, 2013 IP
  11. mumfry

    mumfry Active Member

    Messages:
    118
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #11
    How can i control how many indexes from the array to display
    Lets say there are 5 pockets in the array
    and i only want to display 3
    How can i do so
    i tried doing it like this
    foreach(explode(' ',$thestring,3)){
     
    }
    Code (markup):
    But this never worked
    Still displayed all the array indexes instead of just three as specified in the explode function
    How can i limit it to just three or any specified number
     
    mumfry, Mar 29, 2013 IP