#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    1
    Rep Power
    0

    Regular expression to work with PHP


    Hello,

    I have this regular expression which works in Sublime Text editor:

    \n^[ ]{3}$

    What it does is match 2 lines:

    on the first line it matches the end of the line (so basically any line with string will do)
    on the second line it must have 3 spaces and nothing else (if it has 3 spaces and text or 4 spaces and nothing - does not match the pattern).

    Unfortunately when I try to apply this pattern in PHP the output is still kept unchanged. I figured PHP did not understand the reg ex.
    I tried to create different variations of the same pattern but PHP either complained about an error or simply outputted the result unchanged.

    For simplicity, consider the following text file:

    UID(AB55*****A33921) ALLOW
    UID(CD57428**B9000G) ALLOW

    UID(DE57428**R9000H) ALLOW
    UID(LT57945**B59649) ALLOW

    3rd line contains 3 spaces. The command I issue is this:

    $result = preg_replace("\n^[ ]{3}$^", '', $file); //had to throw ^ to avoid No ending delimiter '^' found

    This command should remove (replace) new line and 3 spaces into nothing. In other words - remove empty lines with 3 spaces so each line contains something.

    Any ideas?
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,885
    Rep Power
    9646
    PHP's PCRE functions require delimiters because that first argument is both the regex and any flags you want to apply. A slash is fairly standard.
    PHP Code:
    preg_replace('/\n^[ ]{3}$/'

IMN logo majestic logo threadwatch logo seochat tools logo