views:

69

answers:

3

Hi guys, how can i get some part of string (in symbols). For example:

$string = 'one two three';
$numSymbols = 7;
 %what should I do%
$res_string = 'one two';

Help, please.

+5  A: 

You are looking for the substr function, I'd say.

In your case, here is an example :

$string = 'one two three';
$numSymbols = 7;

$res_string = substr($string, 0, $numSymbols);
var_dump($res_string);

And you'll get :

string 'one two' (length=7)

Parameters :

  • the string to extract from
  • the position of the first character ; ie, 0, if you want to start at the beginning of the string
  • the number of characters you want : 7
Pascal MARTIN
* "In you*r* case"
arbales
@arbales : that's the problem with trying to type too fast :-( Thanks! I've edited to correct that typo (and a few others... )
Pascal MARTIN
+2  A: 

use php method substr:

$string = 'one two three';
$numSymbols = 7;
$res_string = substr($string, 0, $numSymbols);
Daryl
+2  A: 

You should be using substr()

Example:

$string = 'one two three';
$res_string = substr($string, 0, 7);// $res_string == 'one two'
thephpdeveloper