a123123 Posted August 12, 2011 Share Posted August 12, 2011 Hello. I need to change every string: aa aaa aaaa in general: a{1,} to b - independent of a quantity, there should be only one b. Do you have any ideas to do it? Link to comment https://forums.phpfreaks.com/topic/244587-string-with-various-quantity-of-the-same-element/ Share on other sites More sharing options...
sasa Posted August 12, 2011 Share Posted August 12, 2011 $b = preg_replace('/a+/', 'b', $a); Link to comment https://forums.phpfreaks.com/topic/244587-string-with-various-quantity-of-the-same-element/#findComment-1256312 Share on other sites More sharing options...
a123123 Posted August 12, 2011 Author Share Posted August 12, 2011 Thanks for answer. It works for not special signs. Why doesn't it work with ex. \n (newline)? Link to comment https://forums.phpfreaks.com/topic/244587-string-with-various-quantity-of-the-same-element/#findComment-1256525 Share on other sites More sharing options...
.josh Posted August 12, 2011 Share Posted August 12, 2011 If I had to take a guess, you're probably doing '/\n+/' with single quotes. \n is treated as a literal backslash and n inside single quotes. Use double quotes "/\n+/" Link to comment https://forums.phpfreaks.com/topic/244587-string-with-various-quantity-of-the-same-element/#findComment-1256542 Share on other sites More sharing options...
a123123 Posted August 12, 2011 Author Share Posted August 12, 2011 Thanks for answer. It is the same in both cases ' ' and " ". The problem is about quantity - I need to change every sets of \n, it means \n+, to b letter. It changes, but not to one - one \n changes to one b, two \n to bb, three \n to bbb, and so on. Any way to fix it? Link to comment https://forums.phpfreaks.com/topic/244587-string-with-various-quantity-of-the-same-element/#findComment-1256593 Share on other sites More sharing options...
sasa Posted August 13, 2011 Share Posted August 13, 2011 are you 0n linux or windows? try preg_replace('/(\n\r?\s*)+/', 'b', $a) Link to comment https://forums.phpfreaks.com/topic/244587-string-with-various-quantity-of-the-same-element/#findComment-1256737 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.