cut() public static méthode

Trims at word boundaries (all non-alphanumeric characters are considered delimiters). If the given string is trimmed, an "etc" is added at the end. Its length is also considered.
public static cut ( string $string, integer $max = 50, string $etc = '...' ) : string
$string string Trimmed string
$max integer Maximum length
$etc string "etc" definition
Résultat string
Exemple #1
0
 /**
  * Performs the test.
  *
  * @return \Jyxo\Beholder\Result
  */
 public function run() : \Jyxo\Beholder\Result
 {
     // The \GuzzleHttp library is required
     if (!class_exists(\GuzzleHttp\Client::class)) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Guzzle library missing');
     }
     try {
         $httpClient = new \GuzzleHttp\Client();
         $httpRequest = new \GuzzleHttp\Psr7\Request('GET', $this->url, ['User-Agent' => 'JyxoBeholder']);
         $httpResponse = $httpClient->send($httpRequest, [\GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => 5, \GuzzleHttp\RequestOptions::TIMEOUT => 10]);
         if (200 !== $httpResponse->getStatusCode()) {
             throw new \Exception(sprintf('Http error: %s', $httpResponse->getReasonPhrase()));
         }
         if (isset($this->tests['body'])) {
             $body = (string) $httpResponse->getBody();
             if (strpos($body, $this->tests['body']) === false) {
                 $body = trim(strip_tags($body));
                 throw new \Exception(sprintf('Invalid body: %s', \Jyxo\StringUtil::cut($body, 128)));
             }
         }
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS, $this->url);
     } catch (\Exception $e) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, $e->getMessage());
     }
 }
Exemple #2
0
 /**
  * Performs the test.
  *
  * @return \Jyxo\Beholder\Result
  */
 public function run()
 {
     // The http extension is required
     if (!extension_loaded('http')) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Extension http missing');
     }
     $http = new \HttpRequest($this->url, \HttpRequest::METH_GET, array('connecttimeout' => 5, 'timeout' => 10, 'useragent' => 'JyxoBeholder'));
     try {
         $http->send();
         if (200 !== $http->getResponseCode()) {
             throw new \Exception(sprintf('Http error: %s', $http->getResponseCode()));
         }
         if (isset($this->tests['body'])) {
             $body = $http->getResponseBody();
             if (!preg_match($this->tests['body'], $body)) {
                 $body = trim(strip_tags($body));
                 throw new \Exception(sprintf('Invalid body: %s', \Jyxo\StringUtil::cut($body, 16)));
             }
         }
         // OK
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS);
     } catch (\HttpException $e) {
         $inner = $e;
         while (null !== $inner->innerException) {
             $inner = $inner->innerException;
         }
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, $inner->getMessage());
     } catch (\Exception $e) {
         return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, $e->getMessage());
     }
 }