• support@answerspoint.com

How to get first 5 characters from string

2487

How to get first 5 characters from string using php

$myStr = "HelloWordl";

result should be like this

$result = "Hello";

3Answer


0

For single-byte strings (e.g. US-ASCII, ISO 8859 family, etc.) use substr and for multi-byte strings(e.g. UTF-8, UTF-16, etc.) use mb_substr:

// singlebyte strings
$result = substr($myStr, 0, 5);
// multibyte strings
$result = mb_substr($myStr, 0, 5);

 

  • answered 8 years ago
  • Community  wiki

0

Use substr():

$result = substr($myStr, 0, 5);

 

  • answered 8 years ago
  • Community  wiki

0

If you want to get only one character then use.

$str = 'abcdefghij';

echo $str{5};

 

  • answered 8 years ago
  • Community  wiki

Your Answer

    Facebook Share        
       
  • asked 8 years ago
  • viewed 2487 times
  • active 8 years ago

Best Rated Questions