Skip to content

Conversation

AdsarAdam
Copy link

Functions and supporting tests for substring and case-insensitive subistring functions.

Returns a trim'd portion of the string found between two strings.

@kocsismate kocsismate added the RFC label Jan 13, 2021
@vdelau
Copy link

vdelau commented Jan 13, 2021

The function name substring is very misleading, as I think most people will think of substr and its behaviour. I could see how such a function is useful, but it should be named differently.

@literary-programmer
Copy link

Hello @vdelau, Javascrip too has this name. It has substr and substring both, and people are used to of using these functions. See the following entry in MDN docs. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
Regards

@vdelau
Copy link

vdelau commented Jan 13, 2021

Hi @literary-programmer, the proposed functions would not take integer characters position (or lengths), but strings to look up in the string. The JavaScript functions you mention differ in the sense that they are (start, end) vs (start, length). The proposed function would be more like this:

function substring(string $haystack, string $from, string $to): string
{
    $start = strpos($haystack, $from) + strlen($from);
    $end = strpos($haystack, $to, $start);
    return substr($haystack, $start, $end - $start);
}

(Code is for illustration, not tested or verified against implementation.)

@mikeschinkel
Copy link

I am struggling to think of when I would use these and not just use preg_replace(). In large part because preg_replace() can handle many different match patterns and can support regular expression matching. Rhese functions can only handle one match pattern with no wildcards. I can't remember a time I would use them. #jmtcw

@literary-programmer
Copy link

literary-programmer commented Jan 15, 2021 via email

@php php deleted a comment from Knele83 Jan 18, 2021
@nikic
Copy link
Member

nikic commented Jan 18, 2021

Mailing list thread: https://externals.io/message/112864

@cmb69
Copy link
Member

cmb69 commented Mar 10, 2021

What's the status here? Are you planning to draft an RFC?

@iluuu1994
Copy link
Member

Closing as there was no response.

@iluuu1994 iluuu1994 closed this Apr 17, 2022
@AdsarAdam
Copy link
Author

Hi all - apologies for not replying, completely missed the notifications here.
Anyway - final thoughts were this would be easier supplied in userland as opposed to doing anything in core - thanks all :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants