Michael
Michael

Reputation: 6405

php simple function remove from characters of a word

I need a simple function(e.g function_cut) to cut from a variable characters if the number of characters is bigger than 14. For example the current $user has 18 characters and I need to cut 4 of them (doesn't matter which one).

 $user = "abc123abc123abc123";

 $user = function_cut($user);

Upvotes: 0

Views: 124

Answers (5)

2ndkauboy
2ndkauboy

Reputation: 9387

All the solutions are fine, but if you have UTF8 data, you should use the multibyte versions of the function, namely mb_substr:

function cut($str){ 
  return mb_substr($str, 0, 14); 
}

Upvotes: 1

shamittomar
shamittomar

Reputation: 46692

<?php
    $user = "abc123abc123abc123";
    $user = substr($user, 0, 14);
?>

Upvotes: 1

Aillyn
Aillyn

Reputation: 23793

substr($user, 0, 14)

Upvotes: 2

Sarfraz
Sarfraz

Reputation: 382746

You can use the substr function like this:

function cut($str){
  return substr($str, 0, 14);
}

$user = cut($user);
echo $user;

Upvotes: 0

Kendall Hopkins
Kendall Hopkins

Reputation: 44104

substr

$first_14_characters = substr( $string, 0, 14 );

Upvotes: 5

Related Questions