Function Reference: geodir_substr

Summary

Return part of a string.

Package

GeoDirectory

Parameters

$text
(string) (required) The input string.

Default: None
$start
(int) (required) The position to begin extracting.

Default: None
$length
(int) (required) The desired length.

Default: None
$options
(array) (required) {
An array of options. @type bool $html If true, HTML entities will be handled as decoded characters. @type bool $trimWidth If true, will be truncated with specified width. }.

Default: None

Return Values

(string)

    Change Log

    Since: 1.6.16

    Source File

    geodir_substr() is located in geodirectory-functions/helper_functions.php [Line: 878]

    Source Code

    function geodir_substr($text, $start, $length, array $options) {
        if (empty($options['trimWidth'])) {
            $substr = 'geodir_utf8_substr';
        } else {
            $substr = 'geodir_utf8_strimwidth';
        }
    
        $maxPosition = geodir_strlen($text, array('trimWidth' => false) + $options);
        if ($start < 0) {
            $start += $maxPosition;
            if ($start < 0) {
                $start = 0;
            }
        }
        if ($start >= $maxPosition) {
            return '';
        }
    
        if ($length === null) {
            $length = geodir_strlen($text, $options);
        }
    
        if ($length < 0) {
            $text = geodir_substr($text, $start, null, $options);
            $start = 0;
            $length += geodir_strlen($text, $options);
        }
    
        if ($length <= 0) {
            return '';
        }
    
        if (empty($options['html'])) {
            return (string)$substr($text, $start, $length);
        }
    
        $totalOffset = 0;
        $totalLength = 0;
        $result = '';
    
        $pattern = '/(&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};)/i';
        $parts = preg_split($pattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        foreach ($parts as $part) {
            $offset = 0;
    
            if ($totalOffset < $start) {
                $len = geodir_strlen($part, array('trimWidth' => false) + $options);
                if ($totalOffset + $len <= $start) {
                    $totalOffset += $len;
                    continue;
                }
    
                $offset = $start - $totalOffset;
                $totalOffset = $start;
            }
    
            $len = geodir_strlen($part, $options);
            if ($offset !== 0 || $totalLength + $len > $length) {
                if (strpos($part, '&') === 0 && preg_match($pattern, $part) && $part !== html_entity_decode($part, ENT_HTML5 | ENT_QUOTES, 'UTF-8') ) {
                    // Entities cannot be passed substr.
                    continue;
                }
    
                $part = $substr($part, $offset, $length - $totalLength);
                $len = geodir_strlen($part, $options);
            }
    
            $result .= $part;
            $totalLength += $len;
            if ($totalLength >= $length) {
                break;
            }
        }
    
        return $result;
    }