1

I need to receive a long string with lots of characters and 'cut' it to generate a string with the number of characters I determine, how can I do that?

Example:

$text = 'This is a long string with a lot of characters';

The $text string contains 46 characters in this example.

I need to generate a $newText string with only the 20 first characters, like this:

$newText = 'This is a long strin';
animuson
  • 53,861
  • 28
  • 137
  • 147
Lucas Matos
  • 1,112
  • 5
  • 25
  • 42
  • How exactly 'number of characters i determine' is decided? – cctan Mar 23 '12 at 01:19
  • possible duplicate of [PHP - cut a string after X characters](http://stackoverflow.com/questions/3161816/php-cut-a-string-after-x-characters) – animuson Mar 23 '12 at 01:24
  • the number will be determined by me and will be a fixed number.. Thank you all guys, the substr did the job... – Lucas Matos Mar 23 '12 at 01:25
  • 1
    Surprised that none of the answers have mentioned that plain `substr` is not sufficient for multibyte strings ... You may need to consult [`mb_substr`](http://www.php.net/manual/en/function.mb-substr.php) and/or [`mb_strcut`](http://php.net/manual/en/function.mb-strcut.php) –  Mar 23 '12 at 01:26
  • @rdlowrey, what is multibyte string? is it something about special characters like UTF8? My string contains special characters like á ê ù, should I use mb_substr instead of just substr? – Lucas Matos Mar 23 '12 at 01:29
  • 2
    @LucasMatos The ASCII character set is very limited. You can't use it to display many international characters. Read more here: http://www.php.net/manual/en/intro.mbstring.php –  Mar 23 '12 at 01:35

5 Answers5

5

Not a problem, use substr(). Using your variable names, to get the first 20 characters:

$newText = mb_substr($text, 0, 20, 'UTF-8');

This will get a substring of $text, starting at the beginning, stopping after 20 characters.

<edit>Updated to accomodate @rdlowrey suggestion and OP's character set.</edit>

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
  • 1
    You will do even better to set a variable with your cut string length instead of using a literal like 20. It's good for maintenance, trust me - much easier to to change $string_lenght than find every applicable place you used 20 when you realize you need it to be 25. – Surreal Dreams Mar 23 '12 at 01:48
3

you mean like this?

$newText = substr($text, 0, 20);
dldnh
  • 8,923
  • 3
  • 40
  • 52
3

Have a look at PHP string functions, and in particular, substr:

string substr( string $string, int $start[, int $length])

Returns the portion of string specified by the start and length parameters.

Community
  • 1
  • 1
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
2

Check out strlen and substr.

<?php

$text = 'This is a long string with a lot of characters';
echo 'the $text string contains ' . strlen($text) . ' characters in this example.';
$newText = substr($text, 0, 20);

?>
chrisn
  • 2,095
  • 15
  • 20
0
$newText = mb_substr($text, 0, 20);
Taryn
  • 242,637
  • 56
  • 362
  • 405
Lucas Matos
  • 1,112
  • 5
  • 25
  • 42