The Little Guy Posted July 28, 2008 Share Posted July 28, 2008 Would the best or a possible way to remove 2+ semi-colons in a string be like this? $string = "123;12;12;12;23;;;;142;321;1;132;212;;322"; $replace = ";"; $find = preg_replace("~;{2+}~",$replace,$string); Link to comment https://forums.phpfreaks.com/topic/117023-remove-semi-colons/ Share on other sites More sharing options...
effigy Posted July 28, 2008 Share Posted July 28, 2008 ~;{2,}~ Link to comment https://forums.phpfreaks.com/topic/117023-remove-semi-colons/#findComment-601883 Share on other sites More sharing options...
DarkWater Posted July 28, 2008 Share Posted July 28, 2008 I don't think that regex will work. /({2,}/ That one does. Edit: Someone beat me to it. Link to comment https://forums.phpfreaks.com/topic/117023-remove-semi-colons/#findComment-601884 Share on other sites More sharing options...
sasa Posted July 30, 2008 Share Posted July 30, 2008 <?php $string = "123;12;12;12;23;;;;142;321;1;132;212;;322"; $replace = ""; $find = preg_replace("~(?<=;~",$replace,$string); ?> Link to comment https://forums.phpfreaks.com/topic/117023-remove-semi-colons/#findComment-603825 Share on other sites More sharing options...
corbin Posted July 30, 2008 Share Posted July 30, 2008 sasa why a backreference? Wouldn't the [;]{2,} way be faster? Link to comment https://forums.phpfreaks.com/topic/117023-remove-semi-colons/#findComment-603909 Share on other sites More sharing options...
effigy Posted July 30, 2008 Share Posted July 30, 2008 That's a lookaround (positive lookbehind), and, although I haven't ran any benchmarks, I don't think it's the best approach. ;{2, } is straightforward and--I'm guessing--more efficient. P.S. Don't use useless character classes such as [;]. Link to comment https://forums.phpfreaks.com/topic/117023-remove-semi-colons/#findComment-603921 Share on other sites More sharing options...
corbin Posted July 31, 2008 Share Posted July 31, 2008 lookaround? Oh. (I suck at regexp... Decent at the basics, then I know nothing.) Ah yeah, I guess [;] is entirely pointless x.x. Link to comment https://forums.phpfreaks.com/topic/117023-remove-semi-colons/#findComment-604254 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.