Sunday, September 22, 2024
Google search engine
HomeLanguagesPHP iconv_substr() Function

PHP iconv_substr() Function

The icnov_substr() is an inbuilt function in PHP that allows you to extract a portion of a string based on a specified character encoding. 

Syntax:

iconv_substr( 
    string $string, 
    int $offset, 
    ?int $length = null, 
    ?string $encoding = null 
): string|false

Parameters: This function takes four parameters that are described below.

  • $string: The input string from which you want to extract a portion.
  • $offset: The position of the first character to extract. The position is zero-based, meaning the first character is at position 0, the second character is at position 1, and so on.
  • $length: The length of the portion you want to extract. If this value is negative, the function extracts everything from the start position to the end of the string.
  • $ecoding: The character encoding used in the input string.

Return Values: The return value of iconv_substr() is the extracted portion of the string, or false if the length of the string is less than the offset characters long. In the case when the string’s length is the same as the offset characters long, then the empty string will be returned.

Example 1: The following program demonstrates the iconv_substr() function.

PHP




<?php
$string = "Hello123";
$portion = iconv_substr($string, 2, 3, "UTF-8");
echo $portion;
?>


Output:

llo

Example 2: The following program demonstrates the iconv_substr() function.

PHP




<?php
$string = "1a2b3c4d5e";
$portion = iconv_substr($string, -3, 3, "UTF-8");
echo $portion;
?>


Output:

d5e

Reference: https://www.php.net/manual/en/function.iconv-substr.php

RELATED ARTICLES

Most Popular

Recent Comments