开发者

Regex replace space in string

开发者 https://www.devze.com 2023-03-13 04:31 出处:网络
I\'m messing about with the LESS PHP parser to get it to replace 4 colour hex codes found in IE filters. What I want to do is replace stuff like this: #ff7755 33 with #ff775533 ie. remove all the spac

I'm messing about with the LESS PHP parser to get it to replace 4 colour hex codes found in IE filters. What I want to do is replace stuff like this: #ff7755 33 with #ff775533 ie. remove all the spaces in it. Obviously the characters can vary as they're colour codes. I found this question which is very close to what I want.

Right now, I have this regex which finds the string just fine:

开发者_JAVA技巧

(#([0-9a-f]){6}\s[0-9a-f]{2})

All I need now is the regex to put in the replace argument of preg_replace().


preg_replace('/(#[0-9a-f]{6}) ([0-9a-f]{2})/i','$1$2',$yourSource);


The first example in the PHP manual would seem to be exactly what you are trying to do:

<?php
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);
?>

Of course for you it is:

<?php
$string = '#ff7755 33';
$pattern = '/(#[0-9a-f]{6})\s([0-9a-f]{2})/i';
$replacement = '${1}$2';
echo preg_replace($pattern, $replacement, $string);
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号