Question: How do I insert a new inline style?
mcki0127 opened this issue · 1 comments
mcki0127 commented
I am trying to find every instance of line-height, and add the additional mso line-height style. My code works as expected, except for the final step where the insert is supposed to happen. After the code runs, $text remains unchanged.
$tagArray = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span'];
$pattern = '/(line-height:\s\d+\.?\d+\%?\;)/';
$dom = HtmlDomParser::str_get_html($text); // $text comes from Summernote
// iterate through tags h1, h2, etc.
foreach ($tagArray as $t) {
$tags = $dom->findMulti($t);
// Iterate through all tags of type $t
foreach ($tags as $i => $tag) {
// Check if style with line-height attribute exists
if (!empty($tag->style)
&& str_contains($tag->style, 'line-height')
&& !str_contains($tag->style, 'mso')) {
// Isolate line-height and insert mso style before line-height
preg_match($pattern, $tag->style, $match);
$replace = $this->style('MSOLineHeight') . $this->space . $match[0];
$tag->style = preg_replace($pattern, $replace, $tag->style); <!----- This doesn't work ----->
}
}
}
$tag->style before replace:
string(34) "font-size: 22px; line-height: 1.6;"
$replace:
string(108) "mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; line-height: 1.6;"
$tag->style after replace:
string(125) "font-size: 22px; mso-line-height-rule: exactly; -ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; line-height: 1.6;"
But $text remains unchanged.
mcki0127 commented
Nevermind! I found critical step: $dom->save();