PHPHtmlParser\Content::copyUntil PHP Method

copyUntil() public method

Copy the content until we find the given string.
public copyUntil ( string $string, boolean $char = false, boolean $escape = false ) : string
$string string
$char boolean
$escape boolean
return string
    public function copyUntil($string, $char = false, $escape = false)
    {
        if ($this->pos >= $this->size) {
            // nothing left
            return '';
        }
        if ($escape) {
            $position = $this->pos;
            $found = false;
            while (!$found) {
                $position = strpos($this->content, $string, $position);
                if ($position === false) {
                    // reached the end
                    $found = true;
                    continue;
                }
                if ($this->char($position - 1) == '\\') {
                    // this character is escaped
                    ++$position;
                    continue;
                }
                $found = true;
            }
        } elseif ($char) {
            $position = strcspn($this->content, $string, $this->pos);
            $position += $this->pos;
        } else {
            $position = strpos($this->content, $string, $this->pos);
        }
        if ($position === false) {
            // could not find character, just return the remaining of the content
            $return = substr($this->content, $this->pos, $this->size - $this->pos);
            $this->pos = $this->size;
            return $return;
        }
        if ($position == $this->pos) {
            // we are at the right place
            return '';
        }
        $return = substr($this->content, $this->pos, $position - $this->pos);
        // set the new position
        $this->pos = $position;
        return $return;
    }

Usage Example

示例#1
0
 public function testCopyUntilEscape()
 {
     $content = new Content('foo\\"bar"bax');
     $this->assertEquals('foo\\"bar', $content->copyUntil('"', false, true));
 }
All Usage Examples Of PHPHtmlParser\Content::copyUntil