josem
josem

Reputation: 55

How to use explode to split

I need to split a text when users add a new line and put this new line (by HTML) into an array position.

<?php
$urlstoafiliate = $_POST['urls'];
$affcj = "http://www.an.com";

$arraylinks = explode("/\r\n|\n|\r/", $urlstoafiliate);
echo $arraylinks[ 0 ];

//echo $nombre;
?>

Upvotes: 2

Views: 67

Answers (2)

axiac
axiac

Reputation: 72206

I would use explode("\n", $urlstoafiliate) to get an array of strings and then I would call trim() on each string to remove any trailing \r (if any) but also any leading or trailing spaces (the URLs input by the user may have leading and/or trailing spaces, especially when the user copy/paste them from other sources):

$arraylinks = array_map('trim', explode("\n", $_POST['urls']));

Upvotes: 1

Razib Al Mamun
Razib Al Mamun

Reputation: 2713

You have need preg_split() like this :

<?php
  $urlstoafiliate = $_POST['urls'];
  $arraylinks = preg_split("/\r\n|\n|\r/", $urlstoafiliate);
  print_r($arraylinks);
  // or 
  echo $arraylinks[0]; 
?>

More details : http://php.net/manual/en/function.preg-split.php

Upvotes: 2

Related Questions