PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] how to replace string and preserve char cases in php

 November 20, 2022     php, preg-replace, regex     No comments   

Issue

i have file names strings like this : Admin_studies.php Studies_lang.php

and i want to replace it like this :

Admin_classes.php Classes_lang.php

which keep the case as it's and just replace the sting word

i tried something like this , but it will not keep the original chars cases

$newname = "classes";
$oldname = "studies";
$file_name = "Admin_studies.php";  //or may be $file_name = "Studies_lang.php";
echo preg_replace("/($oldname)/i","$1",$file_name );

Solution

You can set arrays for replacement templates

$newnames = ['classes', 'Classes'];
$oldnames = ['studies', 'Studies'];
$file_name = "Admin_Studies.php";
echo str_replace( $oldnames, $newnames, $file_name );


Answered By - Banzay
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing