Merge branch 'master' of git.umycode.com:dave/PHPWord

This commit is contained in:
Dave Umrysh 2023-04-06 11:42:39 -06:00
commit 427e8439e0
2 changed files with 177 additions and 0 deletions

View File

@ -1347,4 +1347,179 @@ class TemplateProcessor
$str
);
}
// The below is from here: https://pastebin.com/Hq9qmxih
// So I can do this: https://github.com/PHPOffice/PHPWord/issues/984#issuecomment-334967453
/**
* Get a segment. (first segment found)
*
* @param string $needle
* @param string $xmltag
* @param string $docpart
* @param boolean $throwexception
*
* @return string|null
*/
public function getSegment($needle, $xmltag, $docpart = 'MainPart', $throwexception = false)
{
return $this->cloneSegment($needle, $xmltag, $docpart, 1, false, false, $throwexception);
}
/**
* Clone a segment.
*
* @param string $needle
* @param string $xmltag
* @param string $docpart
* @param integer $clones
* @param boolean $replace
* @param boolean $incrementVariables
* @param boolean $throwexception
*
* @return string|null
*/
public function cloneSegment(
$needle,
$xmltag,
$docpart = 'MainPart',
$clones = 1,
$replace = true,
$incrementVariables = true,
$throwexception = false
) {
$needlePos = strpos($this->{"tempDocument$docpart"}, $needle);
if (!$needlePos) {
if ($throwexception) {
throw new Exception(
"Can not find segment '$needle', text not found or text contains markup."
);
} else {
return null; // Segment not found, return null
}
}
$startSegmentStart = $this->findTagLeft("<$xmltag>", $needlePos, $throwexception);
$endSegmentEnd = $this->findTagRight("</$xmltag>", $needlePos);
if (!$startSegmentStart || !$endSegmentEnd) {
if ($throwexception) {
throw new Exception(
"Can not find <$xmltag> around segment '$needle'"
);
} else {
return false;
}
}
$xmlSegment = $this->getSlice($startSegmentStart, $endSegmentEnd);
if ($replace) {
$result = $this->getSlice(0, $startSegmentStart);
for ($i = 1; $i <= $clones; $i++) {
if ($incrementVariables) {
$result .= preg_replace('/\$\{(.*?)\}/', '\${\\1#' . $i . '}', $xmlSegment);
} else {
$result .= $xmlSegment;
}
}
$result .= $this->getSlice($endSegmentEnd);
$this->{"tempDocument$docpart"} = $result;
}
return $xmlSegment;
}
/**
* Replace a segment.
*
* @param string $needle
* @param string $xmltag
* @param string $replacement
* @param string $docpart
* @param boolean $throwexception
*
* @return false on no replacement, true on replacement
*/
public function replaceSegment($needle, $xmltag, $replacement = '', $docpart = 'MainPart', $throwexception = false)
{
$TagPos = strpos($this->{"tempDocument$docpart"}, $needle);
if ($TagPos === false) {
if ($throwexception) {
throw new Exception(
"Can not find segment '$needle', text not found or text contains markup."
);
} else {
return false;
}
}
$SegmentStart = $this->findTagLeft("<$xmltag>", $TagPos, $throwexception);
$SegmentEnd = $this->findTagRight("</$xmltag>", $TagPos);
$this->{"tempDocument$docpart"} =
$this->getSlice(0, $SegmentStart)
. $replacement
. $this->getSlice($SegmentEnd);
return true;
}
/**
* Find the start position of the nearest $tag before $offset.
*
* @param string $tag
* @param integer $offset
* @param boolean $throwexception
*
* @return integer
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
protected function findTagLeft($tag, $offset = 0, $throwexception = false)
{
$tagStart = strrpos(
$this->tempDocumentMainPart,
substr($tag, 0, -1) . ' ',
((strlen($this->tempDocumentMainPart) - $offset) * -1)
);
if (!$tagStart) {
$tagStart = strrpos(
$this->tempDocumentMainPart,
$tag,
((strlen($this->tempDocumentMainPart) - $offset) * -1)
);
}
if (!$tagStart) {
if ($throwexception) {
throw new Exception('Can not find the start position of the item to clone.');
} else {
return 0;
}
}
return $tagStart;
}
/**
* Find the end position of the nearest $tag after $offset.
*
* @param string $tag
* @param integer $offset
*
* @return integer
*/
protected function findTagRight($tag, $offset = 0)
{
$pos = strpos($this->tempDocumentMainPart, $tag, $offset);
if ($pos !== false) {
return $pos + strlen($tag);
} else {
return 0;
}
}
}

View File

@ -83,6 +83,7 @@ class Image extends AbstractElement
$xmlWriter->startElement('w:pict');
$xmlWriter->startElement('v:shape');
$xmlWriter->writeAttribute('type', '#_x0000_t75');
$xmlWriter->writeAttribute('stroked', 'f');
$styleWriter->write();
@ -115,6 +116,7 @@ class Image extends AbstractElement
$xmlWriter->startElement('w:pict');
$xmlWriter->startElement('v:shape');
$xmlWriter->writeAttribute('type', '#_x0000_t75');
$xmlWriter->writeAttribute('stroked', 'f');
$styleWriter->write();