308

How to get first 5 characters from string using php

$myStr = "HelloWordl";

result should be like this

$result = "Hello";
Foreever
  • 7,099
  • 8
  • 53
  • 55
faressoft
  • 19,053
  • 44
  • 104
  • 146

5 Answers5

670

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);
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 3
    Thanks, this is what I needed it for: `if(substr($myURL, 0, 4) == "www.") $myURL = preg_replace('/www./', '', $myURL, 1);` – shanehoban Jun 19 '14 at 09:50
  • 1
    You really only need str_replace('www.','', $url); Dont have to check if it exists. – Doug Cassidy Nov 02 '17 at 16:42
  • 8
    @DougCassidy: what if `$url = "www.subwww.myweirddomainwww.com"`? Without the initial check for `substr($url, 0, 4)`, returned `$url` is ruined!! – Fr0zenFyr Apr 04 '18 at 12:40
  • `preg_replace('/^www\./', '', $myURL, 1);` now it needs to start with www – Michael May 19 '23 at 21:05
56

Use substr():

$result = substr($myStr, 0, 5);
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
28

You can use the substr function like this:

echo substr($myStr, 0, 5);

The second argument to substr is from what position what you want to start and third arguments is for how many characters you want to return.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
24

An alternative way to get only one character.

$str = 'abcdefghij';
echo $str{5};

I would particularly not use this, but for the purpose of education. We can use that to answer the question:

$newString = '';
for ($i = 0; $i < 5; $i++) {
    $newString .= $str{$i};
}
echo $newString;

For anyone using that. Bear in mind curly brace syntax for accessing array elements and string offsets is deprecated from PHP 7.4

More information: https://wiki.php.net/rfc/deprecate_curly_braces_array_access

Paul H.
  • 387
  • 4
  • 8
  • 8
    This is the right answer to the wrong question. Downvoted. – mickmackusa Nov 09 '17 at 03:21
  • 1
    Because it requires more than copy pasting? – Robert Pounder Apr 27 '18 at 12:29
  • NOTE: For multi-byte character sets such as UTF-8, must instead use `mb_substr(i, 1)` to get `i`th character. @RobertPounder - because it doesn't extend to getting more than one character. – ToolmakerSteve Apr 06 '19 at 15:52
  • 2
    @ToolmakerSteve, if you say so; `$string = "Häagen-Dazs"; $stringFirstChars = function($amount, $string) { $i=0;$done=false;$return = '';while($done == false) {$return.=$string{$i};if($amount===$i)$done=true;$i++;}return $return;}; var_dump($stringFirstChars(5, $string));` (copy paste that into phptester.net or w.e) – Robert Pounder Apr 08 '19 at 07:25
  • 1
    Yes I know. I didn't personally downvote it; I just observed in what way it doesn't answer the question. What you wrote - *that* would be a complete answer: showing a beginner how in php one extends from getting one character to multiple characters. Useful addition to the Q&A - thank you. – ToolmakerSteve Apr 08 '19 at 12:25
  • @mickmackusa, in addition to that, since PHP 7.4 curly braces method for accessing array elements has been deprecated – Raffaele D'Elia Aug 27 '20 at 09:20
  • We are going to need more DVers if we are ever going to get the tally down to -1 where it can be deleted. – mickmackusa Aug 27 '20 at 10:59
-3

You can get your result by simply use substr():

Syntax substr(string,start,length)

Example

<?php
$myStr = "HelloWordl";
echo substr($myStr,0,5);
?>

Output :

 Hello
Prabhu Nandan Kumar
  • 1,205
  • 12
  • 22
  • 25
    This VERY late post adds no value and merely bloats the page because everything that it says has already been said (years earlier). Please do not post an answer unless you have something unique and valuable to add. Downvoted this answer to discourage useless, late posts. – mickmackusa Nov 09 '17 at 03:24
  • 2
    @Varin It's ok to post answers 7 years later. Code changes. It just needs to be valuable (not a duplicate) which his wasn't so he got scolded. – Justin Breen Feb 19 '22 at 20:53
  • 5
    @JustinBreen I wrote that comment 4 years ago. Those 4 extra years of experience in the industry made me see how cringe that comment was, which I forgot about, until now. – Varin Feb 22 '22 at 12:18