Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement findBetween(), findBetweenFirst(), and findBetweenLast() Methods #118

Merged

Conversation

salehhashemi1992
Copy link
Contributor

@salehhashemi1992 salehhashemi1992 commented Oct 30, 2023

Implement findBetween(), findBetweenFirst() and findBetweenLast() methods to StringHelper to retrieve a substring that lies between two strings in different scenarios.

findBetween() method retrieves a substring that lies between the first occurrence of a specified start string and the last occurrence of a specified end string in the input string.

Tests are included.

Usage

$htmlContent = "<div class='content'>Hello World</div>";
$content = StringHelper::findBetween($htmlContent, "<div class='content'>", "</div>");
$logData = "INFO: Start --- debug data --- End";
$debugData = StringHelper::findBetween($logData, "Start ---", "--- End");
$htmlData = "<html><body><a href="https://example.com">Link</a></body></html>";
$link= StringHelper::findBetween($htmlData, '<a href="', '">');

findBetweenFirst(): Retrieves the first substring that lies between the specified start and end strings in the input string.
findBetweenLast(): Retrieves the last substring that lies between the specified start and end strings in the input string.

$input = "[a][b][c]";
StringHelper::findBetween($input, "[", "]"); // Output: "a][b][c"
StringHelper::findBetweenFirst($input, "[", "]"); // Output: "a"
StringHelper::findBetweenLast($input, "[", "]"); // Output: "c"

@what-the-diff
Copy link

what-the-diff bot commented Oct 30, 2023

PR Summary

  • New Feature Added to StringHelper Class
    A new method called findBetween has been added to the StringHelper class. This function allows the retrieval of a specific portion of a text string based on the first occurrence of a specified starting string and the last occurrence of an indicated ending string thereafter.

  • Test Case Added for New Feature
    To assure the correct functionality of the new findBetween method, a test case named testFindBetween has been created in the StringHelperTest class. This test will validate the method against various inputs and expected results, making sure it works as intended.

@salehhashemi1992 salehhashemi1992 force-pushed the feature/string-helper-find-between branch from 533b58d to a0da80e Compare October 30, 2023 09:28
@codecov
Copy link

codecov bot commented Oct 30, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (980a5bc) 99.74% compared to head (24f6bbb) 99.76%.
Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #118      +/-   ##
============================================
+ Coverage     99.74%   99.76%   +0.01%     
- Complexity      135      147      +12     
============================================
  Files             7        7              
  Lines           391      424      +33     
============================================
+ Hits            390      423      +33     
  Misses            1        1              
Files Coverage Δ
src/StringHelper.php 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@vjik vjik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

Can you also add new method to readme and add line to changelog?

@salehhashemi1992 salehhashemi1992 force-pushed the feature/string-helper-find-between branch from a0da80e to 15c821b Compare October 31, 2023 18:50
@salehhashemi1992
Copy link
Contributor Author

@vjik Done

Copy link
Member

@Tigrov Tigrov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks should be 2 ways to find a string between two strings:

  1. find to the first position.
  2. find to the last last position.
$htmlContent = "<div class='content'>Hello World</div><div class='content'>It's me</div>";
$content = StringHelper::findBetween($htmlContent, "<div class='content'>", "</div>");

What will be the result?

@salehhashemi1992
Copy link
Contributor Author

salehhashemi1992 commented Nov 1, 2023

It looks should be 2 ways to find a string between two strings:

  1. find to the first position.
  2. find to the last last position.
$htmlContent = "<div class='content'>Hello World</div><div class='content'>It's me</div>";
$content = StringHelper::findBetween($htmlContent, "<div class='content'>", "</div>");

What will be the result?
Duo to the description, it will find the string between the first occurrence of a specified start string and the last occurrence of a specified end string in the input string.
There could be more methods like findBetweenLast and findBetweenFirst that I wanna implement next.
@Tigrov

@Tigrov
Copy link
Member

Tigrov commented Nov 1, 2023

It looks should be 2 ways to find a string between two strings:

  1. find to the first position.
  2. find to the last last position.
$htmlContent = "<div class='content'>Hello World</div><div class='content'>It's me</div>";
$content = StringHelper::findBetween($htmlContent, "<div class='content'>", "</div>");

What will be the result?
Duo to the description, it will find the string between the first occurrence of a specified start string and the last occurrence of a specified end string in the input string.
There could be more methods like findBetweenLast and findBetweenFirst that I wanna implement next.
@Tigrov

Seems findBetweenLast() will be the same as the current findBetween()?

My suggestion to implement both methods in one PR to avoid naming problems.

It could be findBetween() for the first found position and findBetweenLast() for the last found position of argument $end.

@salehhashemi1992
Copy link
Contributor Author

salehhashemi1992 commented Nov 1, 2023

@Tigrov no, they are not the same.
we have three situations:
Consider this: "[a][b][c]"
and the start and end are "[" and "]"

  • findBetween will return "a][b][c"
  • findBetweenFirst will return "a"
  • findBetweenLast will return "c"

src/StringHelper.php Outdated Show resolved Hide resolved
@salehhashemi1992
Copy link
Contributor Author

@Tigrov Thank you. so I add the two other methods in this PR? or a separate one?

@Tigrov
Copy link
Member

Tigrov commented Nov 1, 2023

@Tigrov Thank you. so I add the two other methods in this PR? or a separate one?

👍

Better to add them to this PR to get them together

@vjik vjik added the status:under development Someone is working on a pull request. label Nov 1, 2023
@salehhashemi1992 salehhashemi1992 force-pushed the feature/string-helper-find-between branch from ad29228 to c544d77 Compare November 1, 2023 11:28
@salehhashemi1992 salehhashemi1992 changed the title Implement StringHelper::findBetween Method Implement findBetween, findBetweenFirst(), and findBetweenLast() Methods Nov 1, 2023
@salehhashemi1992 salehhashemi1992 changed the title Implement findBetween, findBetweenFirst(), and findBetweenLast() Methods Implement findBetween(), findBetweenFirst(), and findBetweenLast() Methods Nov 1, 2023
@salehhashemi1992
Copy link
Contributor Author

@Tigrov Done

Copy link
Member

@vjik vjik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also suggest make $end argument optional (?string $end = null) in all new methods. If $end is null, then $end = $start.

CHANGELOG.md Outdated Show resolved Hide resolved
tests/StringHelperTest.php Show resolved Hide resolved
src/StringHelper.php Outdated Show resolved Hide resolved
src/StringHelper.php Outdated Show resolved Hide resolved
@salehhashemi1992 salehhashemi1992 force-pushed the feature/string-helper-find-between branch from 075d69b to a3329c1 Compare November 2, 2023 09:27
@salehhashemi1992 salehhashemi1992 force-pushed the feature/string-helper-find-between branch from a3329c1 to 8227316 Compare November 2, 2023 09:27
CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
@vjik vjik merged commit 4507541 into yiisoft:master Nov 2, 2023
17 of 19 checks passed
@vjik
Copy link
Member

vjik commented Nov 2, 2023

@salehhashemi1992 Thank you for PR 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:under development Someone is working on a pull request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants