John Davidson

php - lowercase only letters between brackets

0 comments
Message:


i am trying to Convert everything between brackets to lowercase letters.


$text = 'AABBCC [AA] AAbbCC [Bb] aabbcc [cc]';
preg_match_all('/\([^)]+\)/', $text, $matches);
foreach ($matches[0] as $match)
{
$match = strtolower($match);
$text = preg_replace(strtolower($match), $match, $text);
}
echo $text;

the output


'AABBCC [AA] AAbbCC [Bb] aabbcc [cc]'

the output i need is


'AABBCC [aa] AAbbCC [bb] aabbcc [cc]'


My Answer:
$text = 'AABBCC [AA] AAbbCC [Bb] aabbcc [cc]';
preg_match_all('/\[(.*?)\]/', $text, $matches);

foreach ($matches[1] as $match) {
$lowercase = strtolower($match);
$text = str_replace("[$match]", "[$lowercase]", $text);
}

echo $text;

// Output: 'AABBCC [aa] AAbbCC [bb] aabbcc [cc]'

Rate this post

4 of 5 based on 9617 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us