PHP : Custom trim function instead existing ltrim() or rtrim() I needed to create a custom trim() function instead of using existing ltrim or rtrim due to below problems.

Here are problem with existing functions.

$string = '&lt;ul&gt;&lt;li&gt;This&lt;/li&gt;&lt;/ul&gt;';

var_dump(ltrim($string, '&lt;br&gt;'));

// Output :: string(21) "ul&gt;&lt;li&gt;This&lt;/li&gt;&lt;/ul&gt;"


Fiddle.

As you can see ltrim trimming &lt; brace because &lt;br&gt; not found I think (Not sure 100%).

Here created function,

function trimString($string, $replaceWith = ' ', $type = 'ltrim')
{
    $type = (!empty($type) &amp;&amp; in_array($type, ['ltrim', 'rtrim'])) ? $type : 'ltrim';
    if (!empty($string) &amp;&amp; !empty($replaceWith)) {
        // Like ltrim
        if ($type == 'ltrim') {
            if (strpos($string, $replaceWith) === 0) {
                $string = substr_replace($string, '', 0, strlen($replaceWith));
            }
        } elseif ($type == 'rtrim') {
            // Like rtrim
            $reverceString    = strrev($string);
            $replaceWithCount = strlen($replaceWith);
            $count            = 1;
            for ($i = 0; $i &lt; strlen($string); $i++){
                if ($count &lt;= $replaceWithCount) {
                    $newString .= $reverceString[$i];
                }
                $count++;

                if ($count == $replaceWith) {
                    break;
                }
            }
            if (!empty($newString)) {
                if (strrev($newString) == $replaceWith) {
                    $string = strrev(strReplaceFirst($reverceString, strrev($replaceWith), ''));
                }
            }
        }
    }

    return $string;
}

function strReplaceFirst($content, $from, $to)
{
    if (!empty($content) &amp;&amp; !empty($from)) {
        $from = '/'.preg_quote($from, '/').'/';

        return preg_replace($from, $to, $content, 1);
    }

    return $content;
}


Fiddle.

So, it is working properly for me! But I still don't know if it's the proper way to do that or not? Or it's side effect and time consumption.
I wonder if you did a quick Google search before rolling your own solution. Despite being implemented via different languages, they all give the same general regex solution.


Regex to remove all &lt;br /&gt; tags from beginning and end of a string in javascript
Remove &lt;br&gt; tags from the beginning and end of a string
Remove BR tag from the beginning and end of a string


As you have discovered, trim() functions treat the 2nd parameter as a "character mask", this means that it will greedily remove any consecutive characters from the beginning/end of the string from that "list of characters" (&lt;br&gt; is the same as rb&gt;&lt;).

If you need a utility function and require the same passed-in parameters:

function trimString($input, $find, $replacement = ' ', $type = 'ltrim') {
    $patterns = [
        'ltrim' =&gt; '^(?:' . preg_quote($find, '/') . ')+',
        'rtrim' =&gt; '(?:' . preg_quote($find, '/') . ')+$'
    ];
    $pattern = $patterns[$type] ?? implode('|', $patterns);
    return preg_replace('/' . $pattern . '/i', $replacement, $input);
}


If you want to be able to pass replacement strings which contain regex to the custom function like &lt;br\s*/?&gt;, then you can remove the preg_quote() calls entirely or set a flag/argument in the custim function which determines whether the quoting call should be implemented.  This means that the responsiblity of quoting any special characters is transferred from the custom function to whatever layer is calling the custom function.

If you want to use trim() before or after the execution of this custom function, that makes perfect sense too -- removing whitespace characters is exactly what it is mean to do.

If your requirements keep extending and you have the intention of removing HTML tags from a valid HTML document, then regex is probably not the right path to go down.  Regex is DOM-unaware and will be vulnerable to breakage in fringe case scenarios -- a legitimate DOM parser would be my strong recommendation in that case.