0

I need a simple regex string from you.

For example, thats my regex example.

preg_replace('#([a-z]+)\:\:' . "([\r\n a-z0-9=]*)" . '#is', '\1:: \2', /* Example string */)

And thats the example string.

dn: CN=Gast,CN=Users,DC=question,DC=local
changetype: add
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: Gast
description:: 
 Vm9yZGVmaW5pZXJ0ZXMgS29udG8gZsO8ciBHYXN0enVncmlmZiBhdWYgZGVuIENvbXB1dGVyIGJ6dy
 4gZGllIERvbcOkbmU=
name: Gast
sAMAccountName: Gast
unicodePwd::IgA2AEcATQBNAHQANwBoADcAIgA=
userAccountControl:512

The result is the following.

dn: CN=Gast,CN=Users,DC=question,DC=local
changetype: add
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: Gast
description:  
 Vm9yZGVmaW5pZXJ0ZXMgS29udG8gZsO8ciBHYXN0enVncmlmZiBhdWYgZGVuIENvbXB1dGVyIGJ6dy
 4gZGllIERvbcOkbmU=
name: Gast
sAMAccountName: Gast
unicodePwd::IgA2AEcATQBNAHQANwBoADcAIgA=
userAccountControl:512

I would have this result.

dn: CN=Gast,CN=Users,DC=question,DC=local
changetype: add
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: Gast
description:: Vm9yZGVmaW5pZXJ0ZXMgS29udG8gZsO8ciBHYXN0enVncmlmZiBhdWYgZGVuIENvbXB1dGVyIGJ6dy4gZGllIERvbcOkbmU=
name: Gast
sAMAccountName: Gast
unicodePwd::IgA2AEcATQBNAHQANwBoADcAIgA=
userAccountControl:512

Then i could do this.

$lines = explode("\r\n", /* Example string */);
foreach($lines as $line)
{
    $tmp = explode(':', $line);
    if(count($tmp) > 2)
    {
        $tmp = explode('::', $line);
        $tmp[1] = base64_decode($tmp[1]);
    }
}

Or is it possible to do this easier??

Thank you in Advance!

1 Answer 1

1

Also this would be possible:

1.) Strip spaces after :: and space-newlines-space

$str = preg_replace('/::\K\s+|\s*\n(?=[^:]*(\n|$))\s*/', "", $str);

Regex explanation at regex101. Strips white-space characters after :: | OR space-newlines-space if no colon exists in the next line (checking with a lookahead).

2.) base64 decode using a callback:

$str = preg_replace_callback('/(?<=::)\S+/',
       function ($m) { return base64_decode($m[0]); },
       $str);

\S+ Non-white-space characters after a :: using a lookbehind, see regex101

3.) Add space after ::

$str = str_replace("::", ":: ", $str);

See example at eval.in


If your PHP version has no support for anonymous functions, swap the function out:

function b64d ($m) { return base64_decode($m[0]); }
$str = preg_replace_callback('~(?<=::)\S+~', "b64d", $str);

Hope this helps!

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

2 Comments

Yes really good!! Thank you. I dont know why i'm not using multiple things like yours. But i dont now for what \K stands and why you use \n and not \r\n ^^
@PatrickB Used \K as a lookbehind here, same as (?<=::)\s+|.... Read more about \K. And to other question: I used only \n because it's preceded by \s* whitch is a shorthand for [ \t\r\n\f] (including \r) and makes it more compatible. Strip spaces with at least one \n newline if not followed by any amount of non-colons and another \n or end. Happy answer works for you :)

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.