Received me an order. As I described the TK:

  1. There is a file in which several articles
  2. Each line is divided by three or more Enter.
  3. The article consists of a heading, first line, and text,
  4. All other lines where paragraphs are less than three ...


Wrote a script in Notepad ++. For breakdown and definition of paragraphs used the symbol \n .

Example (this code broke my text into array elements, divided by three paragraphs):

 $arraytext= explode("\n\n\n", $contents); 

When the customer began to check, it turned out that he did not manually break the file. And it copies the text from the editor - in each line there are incomprehensible spaces. I copied them and skipped through a script to display the character code:

 $i=0; $lines = file('111.txt'); foreach($lines as $single_line){ echo ord($single_line[0]).'<br>'; } 

As a result, I received this list of codes:

10 32 32 32 32 10 32 32 32 32 32 32 32 32 32 32 32 32 32 32

How I see the solution to the problem: replace all characters with codes 10 and 32 with \n , then delete the extra \n and my script will work with the content.

Problem : what is the code of the newline character \n , and how to find and replace codes 10 and 32? As far as I understand, the str_replace function str_replace not work here.

If there are other solutions to my problem, I will be glad to know.

    1 answer 1

    Try breaking up like this:

     preg_split("/(?:\\n[ \\r]*){3,}/, $arr); 

    Splits regardless of the presence of spaces and regardless of the number of line breaks

    • Well, we will not take a typo into account, but everything works !!! Super! Do not tell me where you can read about regulars so that it is more meniey understandable ... I would never make such a construction (((( arashvg
    • J. Friedl Regular Expressions - ReinRaus