Jump to content

Avoid a character for last element


sandy1028

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/227521-avoid-a-character-for-last-element/
Share on other sites

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.