contains() public static method

This method is case sensitive. The need can be an array with multiple chars or words who are going to look up in the haystack string. If an array of needle words is provided the $strict parameter defines whether all need keys must be found in the string to get the true response or if just one of the keys are found the response is already true.
Since: 1.0.0-rc1
public static contains ( string | array $needle, string $haystack, boolean $strict = false ) : boolean
$needle string | array The char or word to find in the $haystack. Can be an array to multi find words or char in the string.
$haystack string The haystack where the $needle string should be looked up.
$strict boolean If an array of needles is provided the $strict parameter defines whether all keys must be found ($strict = true) or just one result must be found ($strict = false).
return boolean If an array of values is provided the response may change depending on $findAll
Ejemplo n.º 1
0
 public function testArrayStrictContains()
 {
     $this->assertTrue(StringHelper::contains(['foo', 'bar'], 'hello foo bar', true));
     // enabled $strict mode
     $this->assertFalse(StringHelper::contains(['notexistings', 'bar'], 'hello bar foo', true));
     // enabled $strict mode
     $this->assertFalse(StringHelper::contains(['bar', 'notexistings'], 'hello bar foo', true));
     // enabled $strict mode
     $this->assertFalse(StringHelper::contains(['notexistings'], 'hello bar foo', true));
     // enabled strict mode
     $this->assertTrue(StringHelper::contains(['a', 'b', 'c'], 'thesmallabc', true));
 }
Ejemplo n.º 2
0
 public function getLinks()
 {
     try {
         $crawler = $this->getCrawler();
         if (!$crawler) {
             return [];
         }
         $links = $crawler->filterXPath('//a')->each(function ($node, $i) {
             return $node->extract(array('_text', 'href'))[0];
         });
         foreach ($links as $key => $item) {
             if (StringHelper::contains(['@'], $item[1])) {
                 unset($links[$key]);
                 continue;
             }
             $url = parse_url($item[1]);
             if (!isset($url['host']) || !isset($url['scheme'])) {
                 $base = $this->baseHost;
             } else {
                 $base = $url['scheme'] . '://' . $url['host'];
             }
             $path = null;
             if (isset($url['path'])) {
                 $path = $url['path'];
             }
             $url = rtrim($base, "/") . "/" . ltrim($path, "/");
             $links[$key][0] = self::cleanupString($links[$key][0]);
             $links[$key][1] = http_build_url($url, ['query' => isset($host['query']) ? $host['query'] : []], HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT);
         }
         return $links;
     } catch (\Exception $e) {
         return [];
     }
 }