Dies war zu knacken ein bisschen tricky Herausforderung! ... aber ich habe es. Negative Lookarounds erwiesen sich als unbrauchbar, um unerwünschte Teilstrings zu negieren. Die (*SKIP)(*FAIL)
Technik hat den Job gemacht.
Die Logik dahinter besteht darin, die drei Arten von Wörtern unabhängig vom Abstand anzusprechen. Sie sind:
- Klein
- Titlecase
- Alle Versalien-
Siehe die Inline-Kommentare im PHP-Code-Block für Laien Muster Erklärung.
Muster: Demo
/(?:\p{Ll}+|\p{Lu}\p{Ll}+|\p{Lu}{2,}+)[,.!?]?(?:\s|$)(*SKIP)(*FAIL)|(?:\p{Ll}+|\p{Lu}{2,}+|\p{Lu}\p{Ll}+)[,.!?]?/u
Code: (Demo)
$input='HelloEveryOne, СаломБаХама, Ҳама дарПеши ҷаҳонЯк мебошадАммо.
HELLOeveryOneHelloFORyouYOU HELLO everyOneHello FORyouYOU
can,BEcorrectedThisSTRINGinCorrectlyFORm
canBEcorrected ThisSTRINGin CorrectlyFORm.';
// optional trailing punctuation-vvvv vvvv- white space or end of input (that we don't want to replace)
var_export(preg_replace('/(?:\p{Ll}+|\p{Lu}\p{Ll}+|\p{Lu}{2,}+)[,.!?]?(?:\s|$)(*SKIP)(*FAIL)|(?:\p{Ll}+|\p{Lu}{2,}+|\p{Lu}\p{Ll}+)[,.!?]?/u','$0 ',$input));
// all lower-^^^^^^^ ^^^^^^^^^^^-all upper ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-repeat first alternative without trailing white space or end of input
// one upper then all lower-^^^^^^^^^^^^^ ^^^^^^^^^^^^^^-discard these matches
Ausgang:
'Hello Every One, Салом Ба Хама, Ҳама дар Пеши ҷаҳон Як мебошад Аммо.
HELLO every One Hello FOR you YOU HELLO every One Hello FOR you YOU
can, BE corrected This STRING in Correctly FOR m
can BE corrected This STRING in Correctly FOR m.'
Sie alle Wörter mit einem Großbuchstaben beginnen? –
Ja! Zum Beispiel, wenn Word mit Großbuchstaben beginnt und mit Groß- oder Kleinbuchstaben und Großbuchstaben kombiniert wird. Beispiel. 'ArrayFunction' oder' ArrayFunction' – John