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
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:
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?
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
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.
$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.
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