Reputation: 8375
This is kind of a silly question and really just for personal interests more than anything, but how would you go about halving multiple newlines?
So lets say I have a series of two \r\n (carriage return/newlines) as a constant, so
Class FOO
{
Const DELIM = "\r\n\r\n";
private fullDelim, halfDelim = false;
public function __construct()
{
$this->fullDelim = self::DELIM;
$this->halfDelim = substr(self::DELIM, -(count(self::DELIM)/2); // or something ??
}
}
Would that be a sane thing to do? can you even substr on newlines? I'm very curious on how you'd go about this in a "sane" way?
Upvotes: 0
Views: 37
Reputation: 780869
Use strlen()
to get the length of a string, not count()
.
$this->halfDelim = substr(self::DELIM, -(strlen(self::DELIM)/2);
Upvotes: 1
Reputation: 8375
Honestly this works, I'm actually a bit surprised....
$half substr(self::DELIM, -(preg_match_all ('/\r\n/', self::DELIM)/2));
Test
echo preg_match_all ('/\r\n/', self::DELIM); // 2
echo count(substr(self::DELIM, -(preg_match_all ('/\r\n/', self::DELIM)/2))); // 1
Upvotes: 0