Problems with PHP regular expressions?

Asked

Viewed 104 times

4

I’m getting a file .txt and excluding the letters and lines in white. It is giving problem with the special character \t or \s he doesn’t recognize.

The code below:

<?php

function pass1() {
    $treat = fopen ("C:\\Users\\Bridge\\Downloads\\D_lotfac\\lott.txt", "r+w+");
    $treat1 = fopen ("C:\\Users\\Bridge\\Downloads\\D_lotfac\\lott1.txt", "r+w+");

    while (!feof ($treat)) {
        $linha = fgets($treat,4096);
        $patterns = array();
        $patterns [0] = '/[(A-Z)i]*/';
        $patterns [1] = '/Â|Ã|Á|À|É|Ê|Í|Î|Ç|Ó|Õ|Ô|Ö|Ú|Û|Ü/';
        $patterns [2] ='/ã|â|à|á|é|ê|í|î|ç|ó|ô|ô|ö|ú|û|ü/';
        $patterns [3] = '/\t/';                 
        $patterns [4] = '/[(a-z)i]*/';
        $patterns [5] = '   ';

        $replacements = array();
        $replacements[] = '';
        $linha = preg_replace($patterns, $replacements, $linha);
        fwrite ($treat1, $linha); 

        printf($linha . "<br>");
        }
}

It’s generating the file lott1.txt correctly, only that the tabs are not being removed nor the spaces (2x, 3x, etc ). I have literally put tab "" or put \t inside the array $pattern[]. Does not eliminate.

What’s the matter?

  • 2

    use a single pattern, to \w, it accepts all characters except the special ones

  • 2

    tried to change the tab to /\t+/

  • @user6855041 put one before and after the file, this can help further understand your doubt?

  • for spaces, use /\s+/ (1 or more occurrences) , /\s{5}/ (5 occurrences)

  • 1

    @lucasvscn I recommend you read What the REGEX shortcut means?

  • @Guilhermelautert thanks for the indication.

Show 1 more comment

1 answer

2


First of all

\s It’s not "space"!!

You can see what the \s here.

Problem

  • From what I noticed you also want to capture accented characters. For this I use modified u, approached here.
  • To capture both uppercase and minuscule can use [a-zA-Z], [[:alpha:]] or [a-z] modifier i.
  • If your intention is to remove all tabs and spaces can do so [\t ]+.

Solution

In summary your Pattern would be :

~[a-z\t ]+~iu

Note

  • [(A-Z)i] - if his intention was to assemble a group with A-Z this does not occur within the [...], the parentheses being interpreted literally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.