如果 Wiktor 的重环顾模式对您来说太难以概念化/维护/适应,那么也许匹配&忽略技术对您来说会更容易。诚然,Wiktor 的模式针对性能进行了优化。
图案:
~ #starting pattern delimiter
\s* #zero or more whitespaces
(?: #start non-capturing group #1
(?: #start non-capturing group #2
\.\d+ #match float expression not requiring leading digits
| #or
\d{1,3}(?:,\d{3})+ #match number containing thousands separators
| #or
ό,τι #match literal greek phrase
| #or
<br\s*/> #match html break tag
) #end non-capturing group #2
(*SKIP)(*FAIL) #discard anything matched by group #2
| #or
( #start capture group #1
\.{3} #match three dots as ellipsis
| #or
[:,.] #match literal colon, comma, or dot
) #end capture group #1
) #end non-capturing group #1
\s* #zero or more whitespaces
~ #ending pattern delimiter
当您希望扩展您的模式以包含更多不合格规则时,只需添加另一个管道并添加一个子模式以匹配不需要的子字符串。
为确保三个符合条件的点作为省略号匹配,请在检查单个字符之前进行匹配。
代码:(Demo)
$text = <<<TEXT
Composition:80% Polyamide, 15% Elastane, 5% Wool.
Side length 50.5 cm <---- THIS SHOULDN'T BE MATCHED
Value 4,500 <---- THIS SHOULDN'T BE MATCHED EITHER
What about $1,234,567.89?
Or....1mm one tenth of a millimeter?
ό,τι<---- THIS IS A FIXED PHRASE IN GREEK AND THEREFORE SHOULDN'T BE MATCHED
Comfort and timeless design characterize the Puma Smash V2 made of suede leather. They can be worn all the time ,being a unique choice for those who want to stand out .Made of rubber.<br />- Softfoam floor<br />- Binding with laces
Specs:<br />• Something<br /><br />• Something else<br />• One more
Children's Form Champion<br /><br />Children's set that will give a comfortable feeling for endless hours of play.<br />It consists of a cardigan and trousers ,made of soft fabric and have rib cuffs and legs for a better fit.<br /><br />• Normal fit<br /><br />• Cardigan :Rib cuffs, zippers throughout length, high neck, Champion logo <br /> <br />• Pants: Elastic waist with drawstring, ribbed legs, Champion logo. Don't worry,there'll be ...more!
TEXT;
echo preg_replace(
'~\s*(?:(?:\.\d+|\d{1,3}(?:,\d{3})+|ό,τι|<br\s*/>)(*SKIP)(*FAIL)|(\.{3}|[:,.]))\s*~',
'$1 ',
$text
);
输出:
Composition: 80% Polyamide, 15% Elastane, 5% Wool. Side length 50.5 cm <---- THIS SHOULDN'T BE MATCHED
Value 4,500 <---- THIS SHOULDN'T BE MATCHED EITHER
What about $1,234,567.89?
Or... .1mm one tenth of a millimeter?
ό,τι<---- THIS IS A FIXED PHRASE IN GREEK AND THEREFORE SHOULDN'T BE MATCHED
Comfort and timeless design characterize the Puma Smash V2 made of suede leather. They can be worn all the time, being a unique choice for those who want to stand out. Made of rubber. <br />- Softfoam floor<br />- Binding with laces
Specs: <br />• Something<br /><br />• Something else<br />• One more
Children's Form Champion<br /><br />Children's set that will give a comfortable feeling for endless hours of play. <br />It consists of a cardigan and trousers, made of soft fabric and have rib cuffs and legs for a better fit. <br /><br />• Normal fit<br /><br />• Cardigan: Rib cuffs, zippers throughout length, high neck, Champion logo <br /> <br />• Pants: Elastic waist with drawstring, ribbed legs, Champion logo. Don't worry, there'll be ...more!