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
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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.