Jump to content

Need result of preg_replace lowercase


mac_gabe

Recommended Posts

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.

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

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]);         
}

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.