How to replace a regular expression with a string

How to use the preg_replace() function to replace a pattern with a string

      $items = 'MBT-6745 MBS-5729'; 
      $items = preg_replace('/MB[ST]/', 'ITEM', $items); 
      echo $items; // Displays ITEM-6745 ITEM-5729e
      

How to use the preg_split() function to split a string on a pattern

       $items = 'MBT-6745 MBS-5729, MBT-6824, and MBS-5214'; 
       $pattern = '/[, ]+(and[ ]*)?/'; 
       $items = preg_split($pattern, $items); 
       
       // $items contains: 'MBT-6745', 'MBS-5729', 'MBT-6824', 'MBS-5214' 
       
       foreach ($items as $item) { 
         echo("<li>$item</li>"); 
       }
      

Description

Back