Skip to content

Commit

Permalink
Add getFindPattern function
Browse files Browse the repository at this point in the history
  • Loading branch information
iranianpep committed Jul 18, 2017
1 parent 4404fd0 commit f9dee39
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Botonomous/utility/StringUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function removeStringFromString($toRemove, $subject)
return trim(preg_replace('!\s+!', ' ', str_replace($toRemove, '', $subject)));
}

private function getFindPattern($toFind, $wordBoundary)
{
return $wordBoundary === true ? "/\b{$toFind}\b/" : "/{$toFind}/";
}

/**
* @param $toFind
* @param $subject
Expand All @@ -46,8 +51,7 @@ public function removeStringFromString($toRemove, $subject)
*/
public function findInString($toFind, $subject, $wordBoundary = true)
{
$pattern = $wordBoundary === true ? "/\b{$toFind}\b/" : "/{$toFind}/";

$pattern = $this->getFindPattern($toFind, $wordBoundary);
return preg_match($pattern, $subject) ? true : false;
}

Expand All @@ -60,8 +64,7 @@ public function findInString($toFind, $subject, $wordBoundary = true)
*/
public function findPositionInString($toFind, $subject, $wordBoundary = true)
{
$pattern = $wordBoundary === true ? "/\b{$toFind}\b/" : "/{$toFind}/";

$pattern = $this->getFindPattern($toFind, $wordBoundary);
$positions = [];
if (preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE) && !empty($matches[0])) {
foreach ($matches[0] as $match) {
Expand Down

0 comments on commit f9dee39

Please sign in to comment.