Exoon
Exoon

Reputation: 1553

PHP seperate URLs in array but not the last one

I want to organize my URLS so they are like this

url1.com
:url2.com
:url3.com

How can i check if its the last entry in the array to make sure it dosen't put a new line and a : after the last URL ?

foreach($urls as $url) {
        $splits .= "$url\n:";
    }

Upvotes: 0

Views: 61

Answers (2)

F. Müller
F. Müller

Reputation: 4062

If your problem is as easy as mentioned then you can use the implode method (as others solutions show). For more complex constructs you may want to use something simmilar to this:

foreach ($urls as $url) {

  if (!end($urls) === $url) {

    // the n-th element
    // ...

  }

  // the last element
  // ...
}

Upvotes: 0

Jessica
Jessica

Reputation: 7005

$string = implode(PHP_EOL.':', $urls);

You should use the PHP_EOL constant so you are not depending on a specific OS.

Upvotes: 3

Related Questions