Advertisement
Advertisement


Remove first 4 characters of a string with PHP


Question

How can I remove the first 4 characters of a string using PHP?

2016/02/08
1
205
2/8/2016 3:28:16 AM

Accepted Answer

You could use the substr function to return a substring starting from the 5th character:

$str = "The quick brown fox jumps over the lazy dog."
$str2 = substr($str, 4); // "quick brown fox jumps over the lazy dog."
2010/11/26
422
11/26/2010 3:23:44 PM

If you’re using a multi-byte character encoding and do not just want to remove the first four bytes like substr does, use the multi-byte counterpart mb_substr. This does of course will also work with single-byte strings.

2010/11/26

function String2Stars($string='',$first=0,$last=0,$rep='*'){
  $begin  = substr($string,0,$first);
  $middle = str_repeat($rep,strlen(substr($string,$first,$last)));
  $end    = substr($string,$last);
  $stars  = $begin.$middle.$end;
  return $stars;
}

example

$string = 'abcdefghijklmnopqrstuvwxyz';
echo String2Stars($string,5,-5);   // abcde****************vwxyz
2014/08/30

You could use the substr function please check following example,

$string1 = "tarunmodi";
$first4 = substr($string1, 4);
echo $first4;

Output: nmodi
2017/03/15

$num = "+918883967576";

$str = substr($num, 3);

echo $str;

Output:8883967576

2018/03/23

You can use this by php function with substr function

<?php
function removeChar($value) {
    $value2 = substr($value, 4); 
    return $value2;
}

echo removeChar("Dummy Text. Sample Text.");
?>

You get this result: " y Text. Sample Text. "

2017/03/23

Source: https://stackoverflow.com/questions/4286423
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]