mac_gabe Posted July 12, 2011 Share Posted July 12, 2011 My failed attempt: <?php $feed[u] = preg_replace("@(A-Z)([A-Z]+)@s",'$1'.strtolower($2).'__', $feed[u]); //replaces AA with Aa__ ?> Hi guys I want to replace AB with Ab__ and ABC with Abc__ and every other Acronym similarly, but I don't know how to write the strtolower() part I need this before sorting an array because the all caps are appearing above the other words. I'll reverse it back to uppercase later. In the version of regex I'm familiar with, I can just do \l$2 to make something lowercase, but I don't think that works with php. Many thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/241811-need-result-of-preg_replace-lowercase/ Share on other sites More sharing options...
AyKay47 Posted July 12, 2011 Share Posted July 12, 2011 I would approach this a different way.. $pattern = "~([A-Z])([A-Z]+)~s"; $arr = array(); preg_match_all($pattern,$subject,$matches); foreach($matches as $value){ $value = ucwords(strtolower($value)); $arr[] = $value; } print_r($arr); Quote Link to comment https://forums.phpfreaks.com/topic/241811-need-result-of-preg_replace-lowercase/#findComment-1241836 Share on other sites More sharing options...
.josh Posted July 12, 2011 Share Posted July 12, 2011 I assume you have an array you are looping through? And I also assume that this is only supposed to affect words that are completely capitalized? I think AyKay47 was on the right track, but this is a more efficient way of doing it. // example array $feed = array('AB','ABC','ABCD','AA','something','foo','bar'); foreach ($feed as $k => $v) { $feed[$k] = preg_replace("~^[A-Z][A-Z]+$~e",'ucfirst(strtolower($0))."__"', $feed[$k]); } Quote Link to comment https://forums.phpfreaks.com/topic/241811-need-result-of-preg_replace-lowercase/#findComment-1241849 Share on other sites More sharing options...
mac_gabe Posted July 12, 2011 Author Share Posted July 12, 2011 Many thanks both of you, I'm going to have a good look through your examples and see what I can learn. In fact though, in the meantime, I have found a solution on the php.net example page for array_multisort(). I don't really understand it but it works beautifully and is very concise. So that others can know how to do this (it seems like an incredibly useful function to me), this is it: $array_lowercase = array_map('strtolower', $feed); array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $feed); there's another simpler version using array_multisort on the php.net sort() page, but that doesn't work for me. I also couldn't get natsort() to work. Yes, it's for looping through an array - sorry should have made that clear. Quote Link to comment https://forums.phpfreaks.com/topic/241811-need-result-of-preg_replace-lowercase/#findComment-1241850 Share on other sites More sharing options...
mac_gabe Posted July 12, 2011 Author Share Posted July 12, 2011 I tried out Crayon's solution, just to learn the syntax, and it does indeed produce the required result. I'll adapt it in the future if I need to put a function on a regex result - so thanks for that. In this instance, there's no requirement now that I have the multisort function, but it will be good to know for the future. Thanks again to you both. Quote Link to comment https://forums.phpfreaks.com/topic/241811-need-result-of-preg_replace-lowercase/#findComment-1241877 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.