php function preg_replace with regex not working, a syntax question
im trying to replace 开发者_如何学运维a remove unnessary new lines with preg-replace but my regex is incorrect. Anyone any ideas whats wrong with my regex? (i have Apache/2.0.54 & PHP/5.2.9
NOW HAVE:
{
blaa {
blow;
blue};
bell;}
}ball
WOULD LIKE:
{blaa {blow;blue};bell;}}ball
These regex dont work, they remove too much or toolitle??
$buffer = preg_replace('#/\}\n|\r|\s/#s', '}', $buffer);
$buffer = preg_replace('#/\{\n|\r|\s/#s', '{', $buffer);
$buffer = preg_replace('#/\;\n|\r|\s/#s', ';', $buffer);
/g (global) gives blanc content and without the # it doestnt do anything. strange?! Anybody any clue why these dont work?
If you want to remove any whitespace after {
, }
, and ;
, do this:
preg_replace('/([{};])\s+/', '$1', $buffer)
Here /
are the delimiters; ([{};])
describes one character of {
, }
, and ;
while the match is captured; and \s+
describes any following whitespace characters (already including \r
and \n
).
This works for me:
$buffer = preg_replace('/\}\\n|\\r|\\s/', '', $buffer);
$buffer = preg_replace('#([{};])(?:\n|\r|\s)#s', '$1', $buffer);
精彩评论