Reputation: 3265
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!
Upvotes: 0
Views: 147
Reputation: 12389
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);
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!
Upvotes: 1