Skip to content

Commit b39ee4c

Browse files
committed
add after and before methods
1 parent 25b7318 commit b39ee4c

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/PhpStringHelpers.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,17 @@ function is_ipv6(string $ip): bool
398398
return strHelpers::is_ipv6($ip);
399399
}
400400
}
401+
402+
if (!function_exists('after')) {
403+
function after(string $string, string $search): string
404+
{
405+
return strHelpers::after($string, $search);
406+
}
407+
}
408+
409+
if (!function_exists('before')) {
410+
function before(string $string, string $search): string
411+
{
412+
return strHelpers::before($string, $search);
413+
}
414+
}

src/utility/StrUtility.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,4 +837,52 @@ public function is_ipv6(string $ip): bool
837837
return false;
838838
return true;
839839
}
840+
841+
/**
842+
* Deletes the words before the given $search word
843+
*
844+
* @param string $string
845+
* @param string $search
846+
* @return string
847+
*/
848+
public function after(string $string, string $search): string
849+
{
850+
if (!$this->checkStringForRemoveOperation($string, $search))
851+
return $string;
852+
return $this->rmExtraBlank(explode($search, $string)[1]);
853+
}
854+
855+
/**
856+
* Deletes the words after the given $search word
857+
*
858+
* @param string $string
859+
* @param string $search
860+
* @return string
861+
*/
862+
public function before(string $string, string $search): string
863+
{
864+
if (!$this->checkStringForRemoveOperation($string, $search))
865+
return $string;
866+
return $this->rmExtraBlank(strstr($string, $search, true));
867+
}
868+
869+
private function checkStringForRemoveOperation(string $string, string $word): bool
870+
{
871+
$string = strtolower(trim($string));
872+
$word = strtolower(trim($word));
873+
874+
if (empty($string) || !is_string($string))
875+
return false;
876+
877+
if (empty($word) || !is_string($word))
878+
return false;
879+
880+
if (!$this->isContains($string, $word))
881+
return false;
882+
883+
if (str_ends_with($string, $word))
884+
return false;
885+
886+
return true;
887+
}
840888
}

0 commit comments

Comments
 (0)