PHP Get First 5 Characters (PGF5C)

How to get first 5 characters of a string using php

I have a case study to display some information from a string, let's just simulate it with the sentence "Hello World".

$myStr = "Hello Wordl";

Hope to get results like this

$result = "Hello";

Solutip

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);

Post a Comment

Previous Next

نموذج الاتصال