PHP in HindiProgramming Language

How to Replace Substring in PHP?

Problem:- How to Replace Substring in PHP? You want to replace a substring with a different string. Example you want to show only 4 digit of a credit card number before printing it.

Solution – Replace Substring in PHP

<?php
$credit_card_no = '1234 5678 9123 9582';
$mask_credit_card_no = substr_replace($credit_card_no, 'xxxx ', 0,strlen($credit_card_no)-4);
print $mask_credit_card_no;
?>

How it Works?

substr_replace($oldstring, $new, $start, $end );

This function replace $old string with $new string and $start variable where you want to start to change and $end where you want to end. about code will print xxxx 9582.

 

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button
Close