sandy1028 Posted February 13, 2011 Share Posted February 13, 2011 Array ( [0] => Virender Sehwag|||http://abc.xyz.com/Virender-Sehwag [1] => Sachin Tendulkar|||http://abc.xyz.com/Sachin-Tendulkar ) foreach($array as $arr) { $topics=explode ('|||', $array); echo '<li>' .'<a rel="nofollow" target="_new" href="'.$topics[1].'"><span class="title">'.html_entity_decode($topics[0], ENT_NOQUOTES, "UTF-8").'</span></a>|</li>'; How to avoid '|' for the last element of any aaray. Also if array contains only 1 element '|' should not appear. For eample, In the browser it should display as "Virender Sehwag | Sachin Tendulkar". If the array contains only 1 element then, In the browser it should display as Virender Sehwag if array contains 3 elements, Virender Sehwag | Sachin Tendulkar | someword Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 13, 2011 Share Posted February 13, 2011 The solution, in large part, is based upon how your originally construct the array. ie reading a csv file into an array will often have empty 'fields' thus there will be delimiters with nothing between them. IF, on the other hand, you are creating the array from a database query, you control when and where to place the delimiter. In your example array there are ONLY triple pipes, AND you explode based upon triple pipes therefore you should never see ANY the pipes when displaying the exploded array. You may have empty element, but you will not see the delimiter. Make sense? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.