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

Sunday, November 20, 2022

[FIXED] How to remove 2 last characters with preg_replace?

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

Issue

I have a code like : 784XX . XX could be a character or number and I need an expression to remove the last 2 characters (XX) using ( and only ) preg_replace.

How can I do that?

For example, the output of :

782A3 is 782,

0012122 is 00121,

76542A is 7654,

333333CD is 333333,

Solution

You can use substr function.

But if you will use preg_replace you can do this:

$val = preg_replace('/[\w\d]{2}$/', '', $val);



Answered By - Vitalii
Answer Checked By - Terry (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