Forum


Replies: 3   Views: 16
Replacevariable to link

Posted by Exorion  · 11-03-2026 - 12:20

foreach ($docx->getTemplateVariables()['document'] as $key => $value) {

$array = $this->getWordStyles($value);

Log::notice('getWordStyles', $array);

$link = new WordFragment($this->docx, 'document');

$style = array(

'fontSize' => $array['fontSize'],

'font' => $array['font'],

'color' => $array['color'],

'bold' => $array['bold'],

'italic' => ($array['italic'] == true) ? true : null,

'strikeThrough' => ($array['strike'] == true) ? true : null,

);

$this->docx->createCharacterStyle('leaderCrmStyle' . $key, $style);

$link->addLink($writestr, array(

'url' => (is_null($this->client) ? env('APP_URL', 'www.site.com') : htmlspecialchars(env('APP_URL', 'https://www.site.com') . '/guest/product_request/' . $this->client->api_token . '/' . $p->id . '/?user_id=' . $this->user_id . '&price_level_id=' . $price_level_id)),

'rStyle' => 'leaderCrmStyle' . $key,

));

$variables = array($value => $link);

$this->docx->replaceVariableByWordFragment($variables, array('type' => 'inline'));

}

In previos phpdocx all was same style like in document - but now - its blue color and underline - how i can set the same style like in document? Thanks

 

Posted by admin  · 11-03-2026 - 14:10

Hello,

All versions of phpdocx, including the oldest ones, apply the following default inline styles to links:

color: 0000ff
underline: single

These styles (blue color and single underline) match the default styles used by MS Word when a hyperlink is added.

Please note that inline styles can't be overridden using a custom character style (rPr styles have a higher priority). You can set a custom color and underline style with the available options (https://www.phpdocx.com/api-documentation/word-content/insert-link-Word-document-with-PHP) as a link added as regular content:

$docx->addLink('A link', ['url' => 'https://www.phpdocx.com', 'color' => '000000', 'underline' => 'none']);

or in a WordFragment:

$linkFragment = new WordFragment($docx);
$linkFragment->addLink('A link', ['url' => 'https://www.phpdocx.com', 'color' => '000000', 'underline' => 'none']);

Premium licenses also include the stylesReplacementType option in replaceVariableByWordFragment and replaceVariableByHTML methods, which allows placeholder styles to be automatically used and mixed during replacements. We recommend upgrading to Premium to use this option.

Regards.

Posted by Exorion  · 11-03-2026 - 16:38

Is it possible to get color of variable replaceble fragment?

Posted by admin  · 11-03-2026 - 16:46

Hello,

You can use the getWordStyles method to get content styles, but this requires parsing the information returned by this method.

To apply or mix placeholder styles with new content when using template methods, the best approach is to use the stylesReplacementType option available in the replaceVariableByWordFragment and replaceVariableByHTML methods. This option allows placeholder styles to be automatically applied and combined during replacements. As detailed in the documentation, this option is available only with a Premium license. You can upgrade to Premium on the "MY PHPDOCX" page after logging in.

Regards.