用phpword中的setValue使单词大胆

发布于 2025-01-19 05:43:28 字数 732 浏览 3 评论 0原文

在PHPWord中使用TemplateProcessor时如何使替换的单词加粗并加下划线?发现的解决方案非常古老(2016)。

我尝试创建我的函数来编辑 xml 文档,但完成的文件崩溃了。

我的代码:

$path = public_path()."/example/dogovor.docx";

$document = new TemplateProcessor($path);

$document->setValue('student-name', 'Alex');

$document->saveAs(public_path("documents/test.docx"));

尝试过这个功能:

private function makeWordBold($str, $hex=null, $style='b'): string
    {
        $cor = $hex ? "<w:color w:val='".$hex."'/>" : "";
        $before="/w:t/w:r<w:r><w:rPr>".$cor."<w:".$style."/>/w:rPr<w:t xml:space='preserve'>";
        $after='/w:t/w:r<w:r><w:t>';
        return $before.$str.$after;
    }

How to make the replaced word bold and underline when using TemplateProcessor in PHPWord? Found solutions are very old (2016).

I tried to create my function, to edit xml document, but done file is crashing.

My code:

$path = public_path()."/example/dogovor.docx";

$document = new TemplateProcessor($path);

$document->setValue('student-name', 'Alex');

$document->saveAs(public_path("documents/test.docx"));

Tried this function:

private function makeWordBold($str, $hex=null, $style='b'): string
    {
        $cor = $hex ? "<w:color w:val='".$hex."'/>" : "";
        $before="/w:t/w:r<w:r><w:rPr>".$cor."<w:".$style."/>/w:rPr<w:t xml:space='preserve'>";
        $after='/w:t/w:r<w:r><w:t>';
        return $before.$str.$after;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛放△進行李 2025-01-26 05:43:28
$path = public_path()."/example/dogovor.docx";

$document = new TemplateProcessor($path);
$word = new TextRun();
$word->addText('Alex', array('underline' => 'single', 'bold' => true));
$document->setComplexValue('student-name', $word);
$document->saveAs(public_path("documents/test.docx"));
$path = public_path()."/example/dogovor.docx";

$document = new TemplateProcessor($path);
$word = new TextRun();
$word->addText('Alex', array('underline' => 'single', 'bold' => true));
$document->setComplexValue('student-name', $word);
$document->saveAs(public_path("documents/test.docx"));
酒绊 2025-01-26 05:43:28

我尝试编写自己的代码,因此它可以处理一个句子中存在多个 标记或也有 标记的情况。我的想法是制作自己独特的标签来替换 HTML 标签。

$yourInputString = "The last word of this sentence is <strong>bold</strong>";

$yourInputString = str_replace("<strong>","|@@|CUSTOMID=01", $yourInputString);
$yourInputString = str_replace("</strong>","|@@|", $yourInputString);

$yourInputString = str_replace("<em>","|@@|CUSTOMID=02", $yourInputString);
$yourInputString = str_replace("</em>","|@@|", $yourInputString);

// $yourInputString = "The last word of this sentence is |@@|CUSTOMID=01bold|@@|"

$exploded = explode("|@@|", $yourInputString);

/* $exploded = [
    'The last word of this sentence is',
    'CUSTOMID=01bold',
    '',
]
*/

$sentence = new TextRun();

for ($j=0; $j < count($exploded); $j++) {
    // I take 11 first character and check whether it's bold or italic
    if (substr($exploded[$j],0,11) == 'CUSTOMID=01') {
        //BOLD TEXT
        $sentence->addText(substr($exploded[$j],11), array('name' => 'Tahoma', 'size' => '11', 'bold' => true));
    }elseif (substr($exploded[$j],0,11) == 'CUSTOMID=02') {
        //ITALIC TEXT
        $sentence->addText(substr($exploded[$j],11), array('name' => 'Tahoma', 'size' => '11', 'italic' => true));
    }else{
        //DEFAULT TEXT
        $sentence->addText(substr($exploded[$j],0), array('name' => 'Tahoma', 'size' => '11'));
    }
}

$templateProcessor->setComplexValue('some_placeholder', $sentence);

I tried to make my own code, so it can handle if there are more than one <strong> tag or there is <em> tag too in one sentence. The idea is I make my own unique tags to replace the HTML tags.

$yourInputString = "The last word of this sentence is <strong>bold</strong>";

$yourInputString = str_replace("<strong>","|@@|CUSTOMID=01", $yourInputString);
$yourInputString = str_replace("</strong>","|@@|", $yourInputString);

$yourInputString = str_replace("<em>","|@@|CUSTOMID=02", $yourInputString);
$yourInputString = str_replace("</em>","|@@|", $yourInputString);

// $yourInputString = "The last word of this sentence is |@@|CUSTOMID=01bold|@@|"

$exploded = explode("|@@|", $yourInputString);

/* $exploded = [
    'The last word of this sentence is',
    'CUSTOMID=01bold',
    '',
]
*/

$sentence = new TextRun();

for ($j=0; $j < count($exploded); $j++) {
    // I take 11 first character and check whether it's bold or italic
    if (substr($exploded[$j],0,11) == 'CUSTOMID=01') {
        //BOLD TEXT
        $sentence->addText(substr($exploded[$j],11), array('name' => 'Tahoma', 'size' => '11', 'bold' => true));
    }elseif (substr($exploded[$j],0,11) == 'CUSTOMID=02') {
        //ITALIC TEXT
        $sentence->addText(substr($exploded[$j],11), array('name' => 'Tahoma', 'size' => '11', 'italic' => true));
    }else{
        //DEFAULT TEXT
        $sentence->addText(substr($exploded[$j],0), array('name' => 'Tahoma', 'size' => '11'));
    }
}

$templateProcessor->setComplexValue('some_placeholder', $sentence);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文