Skip to content

Commit

Permalink
New functions: str_right() and str_left()
Browse files Browse the repository at this point in the history
  • Loading branch information
repat committed Apr 12, 2019
1 parent 63cdabc commit e700704
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,17 @@ base64_url_decode('aHR0cHM6Ly9yZXBhdC5kZQ==');
// returns: https://repat.de
```

#### `str_right($string, $until)`
Substring from the right until given string, see also `str_left()`. Will return the input string if `$until` is not present.

```php
str_right('https://vimeo.com/165053513', '/');
// returns: 165053513
```

#### `str_left($string, $before)`
Syntactic sugar for [`str_before`](https://laravel.com/docs/5.8/helpers#method-str-before) to be consistent with `str_right()`.

### Optional Packages
Optional packages suggested by this are required for these functions to work.

Expand Down Expand Up @@ -696,7 +707,7 @@ extract_inline_img("<img src='data:image/jpeg;base64,...>", '/var/www/htdocs/lar
* MIT, see [LICENSE](https://github.com/repat/laravel-helper/blob/master/LICENSE)
## Version
* Version 0.1.23
* Version 0.1.24
## Contact
#### repat
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["laravel", "helper", "files", "array", "database", "strings", "date"],
"homepage": "http://repat.de",
"license": "MIT",
"version" : "0.1.23",
"version" : "0.1.24",
"authors": [
{"name": "repat", "email": "repat@repat.de"}
],
Expand Down
34 changes: 34 additions & 0 deletions src/string_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,37 @@ function str_remove(string $string, $remove) : ?string
return str_replace($remove, '', $string);
}
}

if (!function_exists('str_right')) {
/**
* String from the right until given string, see also `str_left()`
*
* @param string $string
* @param string $until
* @return string
*/
function str_right(string $string, string $until) : string
{
$pos = strrpos($string, $until);

if ($pos !== false) {
return substr($string, strrpos($string, $until) + 1);
}

return $string;
}
}

if (!function_exists('str_left')) {
/**
* Syntactic sugar for `str_before()` to be consistent with `str_right()`
*
* @param string $string
* @param string $before
* @return string
*/
function str_left(string $string, string $before) : string
{
return str_before($string, $before);
}
}

0 comments on commit e700704

Please sign in to comment.