Home > OS >  Lower case all words inside square brackets in a string PHP
Lower case all words inside square brackets in a string PHP

Time:07-18

I have a string in PHP like 'Hello [FIRSTNAME] [LASTNAME]' I want to convert all characters inside brackets into lower case

CodePudding user response:

You can use preg_replace_callback:

<?php
$text="Hello [FIRSTNAME]";
echo preg_replace_callback('/\b([^]] )\b/', function ($word) {
      return strtolower($word[1]);
 }, $text);
  • Related