12

I would like to explode a string (separated based on a delimiter and placed into an array), using both a space (" ") and a newline ("\n") as a delimiter.

The method (which I don't think is the best way) is to:

  1. Explode the array by spaces
  2. Remake the string from the array
  3. Explode it again for new lines
  4. MySQL escape the individual elements.

Question: How do I exploded a string by both a space and new line?

Reference to: new line Array

Community
  • 1
  • 1
tread
  • 10,133
  • 17
  • 95
  • 170
  • You can use regular expressions which is probably most efficient here. The other way is to explode twice, first by spaces and then each resulting array by new line character. At last, join all arrays into one (array_merge) – Voitcus Jul 29 '13 at 07:39
  • 2
    Use this : `preg_split('/[\s\n]/', $str );` – Prasanth Bendra Jul 29 '13 at 07:48

4 Answers4

25

You could just do a

$segments = preg_split('/[\s]+/', $string);

This function will split $string at every whitespace (\s) occurrence, including spaces, tabs and newlines. Multiple sequential white spaces will count as one (e.g. "hello, \n\t \n\nworld!" will be split in hello, and world! only, without empty strings in the middle.

See function reference here.

Avatar
  • 14,622
  • 9
  • 119
  • 198
Paolo Stefan
  • 10,112
  • 5
  • 45
  • 64
  • I've been searching absolutely everywhere using preg_match, str_replace all sorts to find this perfect solution. Thank you very much. – TLG123 Apr 17 '20 at 08:19
  • It also removes multiple empty lines. Nice. No need to go over the resulting array and find empty slots. – Avatar Jul 16 '21 at 06:11
7

You can use preg_split to explode the content using multiple delimiter

$pattern = '/[ \n]/';
$string = "something here ; and there, oh,that's all!";
echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>'; 
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
1

Always go from the bigger to the smaller one.

So first split by "\n" and then split by " ".

$data = "This is a test\nAnd something new happens.";
$rows = explode("\n", $data);
$words = array();
foreach($rows as $row) {
    $temp = explode(" ", $row);
    foreach($temp as $word)
        $words[] = $word;
}

Will give you an array with all words in it.

Tobias Golbs
  • 4,586
  • 3
  • 28
  • 49
1

You can use to explode the content using multiple delimiter.

Sample code

<?php
$str = "Brand : Pandora~ Collection : Summer 13~ Colour : Multi~ Gender : Ladies~ Material : Murano Glass~ Our Code : 0573835~ Packaging : Pandora Branded Packaging~ Product Code : 750513~ Product Type : Bead~ Style : Contemporary";
$r1 = explode("~", $str);
$stringtxt = '<table>';
if (count($r1) > 0) {
    foreach ($r1 as $value) {
        $rows = explode(":", $value);
        $stringtxt .= '<tr>';
        $stringtxt .= '<td>' . $rows[0] . '</td><td>' . $rows[1] . '</td>';
        $stringtxt .= '</tr>';
    }
}
$stringtxt .= '</table>';
print $stringtxt;
?>

Output

<table>
<tr><td>Brand </td><td> Pandora</td></tr>
<tr><td> Collection </td><td> Summer 13</td></tr>
<tr><td> Colour </td><td> Multi</td></tr>
<tr><td> Gender </td><td> Ladies</td></tr>
<tr><td> Material </td><td> Murano Glass</td></tr>
<tr><td> Our Code </td><td> 0573835</td></tr>
<tr><td> Packaging </td><td> Pandora Branded Packaging</td></tr>
<tr><td> Product Code </td><td> 750513</td></tr>
<tr><td> Product Type </td><td> Bead</td></tr>
<tr><td> Style </td><td> Contemporary</td></tr>
</table>
stalinrajindian
  • 1,363
  • 1
  • 14
  • 22