0

here is the content inside a mysql table - column type text:

lorem ipsum

* * *

dolor sit

I got it using php as a string variable and want to explode it by two consecutive line ends

$arr = explode("\n\n", $str);
echo count($arr) // 3 

Now, another row has the same content and echo count($arr) gives 1

I suppose something is wrong with end of lines, but how can I check this and how to ensure that in the future all end of lines to be the same?

user3783243
  • 5,368
  • 5
  • 22
  • 41
provance
  • 877
  • 6
  • 10
  • Please make runnable case – Marcin Orlowski Apr 21 '23 at 12:37
  • 2
    _"Now, anothe row has the same content"_ - "looks the same when rendered as HTML", does not have to mean _is_ exactly the same. The most likely explanation here would probably be, that this one is using `\r\n` for line breaks. – CBroe Apr 21 '23 at 12:45
  • Debug your code. For example, use `var_dump($str);` to see the actual, literal content of the string (not just how it's interpreted by a HTML parser (i.e. your browser). Then you can predict what will happen to it when you run explode(). – ADyson Apr 21 '23 at 12:46
  • 1
    _"and how to ensure that in the future all end of lines to be the same?"_ - [How to replace different newline styles in PHP the smartest way?](https://stackoverflow.com/q/7836632/1427878) – CBroe Apr 21 '23 at 12:46
  • `echo rawurlencode($str)` also helps to debug what exactly you're looking at. – deceze Apr 21 '23 at 12:48
  • @CBroe - How to unify all ends of lines in a string? I cannot use `PP_EOL` because I need two consecutive ends – provance Apr 21 '23 at 13:56
  • 1
    _"I cannot use PP_EOL because I need two consecutive ends"_ - `PHP_EOL` is _one_ line break, in a OS-specific format. So why could you not use this then, if you needed two consecutive line breaks? That would be _one_ `PHP_EOL`, immediately followed by a _second_ `PHP_EOL` then. Why do you think that would be _any_ different, from having two line breaks explicitly expressed as `\n\n` in the first place? – CBroe Apr 21 '23 at 14:00
  • @CBroe - I need to explode the string by two consecutive ends. `$arr = explode(PHP_EOLPHP_EOL, $str);` - will not work – provance Apr 21 '23 at 14:03
  • 1
    Of course not, because a constant named `PHP_EOLPHP_EOL` does not exist. How do we concatenate two values again in PHP, in a string context ...? `PHP_EOL.PHP_EOL` – CBroe Apr 21 '23 at 14:05
  • @CBroe - I tried `$arr = explode(PHP_EOL.PHP_EOL, $str);` The first string is exploded correctly (3 items), and the second - again just 1 item. Something is deeply wrong with end of lines in that column – provance Apr 21 '23 at 14:12
  • 1
    Don't guess, *see.* Again: `echo rawurlencode($str)`. – deceze Apr 21 '23 at 14:12
  • @deceze - I tried `rawurlencode`. In the first string end of lines are `%0D%0A`. Second - `%0A`. How to unify them ? – provance Apr 21 '23 at 14:33
  • `$str = str_replace("\r", '', $str)`…? – deceze Apr 21 '23 at 14:43
  • @deceze - It works finally. Thanks a lot. Give me please some reference link where I can see that `%0D%0A` means `\r` and similar – provance Apr 21 '23 at 14:52
  • 1
    https://en.wikipedia.org/wiki/ASCII#Character_set – deceze Apr 21 '23 at 15:05
  • 1
    https://en.wikipedia.org/wiki/Newline#Representation – ADyson Apr 21 '23 at 15:11

0 Answers0