1

This is the string I'm trying to explode. This string is part of a paragraph which i need to split at every "newline,space,newline,space" :

s

 1

A result from textmagic.com show it contains a \n then a space then a \n and then a space.

enter image description here

This is what I tried:

$values = explode("\n\s\n\s",$string); // 1
$values = explode("\n \n ",$string);   // 2
$values = explode("\n\r\n\r",$string); // 3

Desired output:

Array (
    [0] => s
    [1] => 1
)

but none of them worked. What's wrong here? How do I do it?

5
  • What is the desired output here? Commented Jun 19, 2019 at 6:26
  • I've added desired output to the question. btw i reverted back some of the edit that you made because it changes the spaces and newlines in the string. Commented Jun 19, 2019 at 6:30
  • My guess is that you are dealing with Windows newlines, so the proper string would become \n\r \n\r , but I haven't tested it, as I'm typing this comment from my mobile Commented Jun 19, 2019 at 6:37
  • Where exactly do you think it shows in that textmagic output that it was \n? The legend explicitly says that the yellow-ish symbols are counted as two characters, so that would make it much more likely that this is indeed \r\n. Plus, if you are not sure what your data actually contains, using urlencode is a quick way to get to see those “invisible” byte sequences. Commented Jun 19, 2019 at 6:50
  • There might be a problem with your interpretation of a 'newline' character, the representation of it depends on the operating system and character set encoding. So "\n" might not match the 'newline' character of your data, binary wise. See en.wikipedia.org/wiki/Newline . The screenshot of TextMagic says that the newline character in the data takes 2 bytes, which should point you in the right direction. Commented Jun 19, 2019 at 7:01

2 Answers 2

2

Just use explode() with PHP_EOL." ".PHP_EOL." ", which is of the format "newline, space, newline, space". Using PHP_EOL, you get the correct newline-format for your system.

$split = explode(PHP_EOL." ".PHP_EOL." ", $string);
print_r($split);

Live demo at https://3v4l.org/WpYrJ

Sign up to request clarification or add additional context in comments.

Comments

0

Using preg_split() to explode() by multiple delimiters in PHP

Just a quick note here. To explode() a string using multiple delimiters in PHP you will have to make use of the regular expressions. Use pipe character to separate your delimiters.

$string = "\n\ranystring"
$chunks = preg_split('/(de1|del2|del3)/',$string,-1, PREG_SPLIT_NO_EMPTY);

// Print_r to check response output.
echo '<pre>';
print_r($chunks);
echo '</pre>';

PREG_SPLIT_NO_EMPTY – To return only non-empty pieces.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.