public function f_html2docx($currVal) {
// handling <i> tag
$el = 'i';
$tag_open = '<' . $el . '>';
$tag_close = '</' . $el . '>';
$nb = substr_count($currVal, $tag_open);
if ( ($nb > 0) && ($nb == substr_count($currVal, $tag_open)) ) {
$currVal= str_replace($tag_open, '</w:t></w:r><w:r><w:rPr><w:i/></w:rPr><w:t>', $currVal);
$currVal= str_replace($tag_close, '</w:t></w:r><w:r><w:t>', $currVal);
}
// handling <b> tag
$el = 'b';
$tag_open = '<' . $el . '>';
$tag_close = '</' . $el . '>';
$nb = substr_count($currVal, $tag_open);
if ( ($nb > 0) && ($nb == substr_count($currVal, $tag_open)) ) {
$currVal= str_replace($tag_open, '</w:t></w:r><w:r><w:rPr><w:b/></w:rPr><w:t>', $currVal);
$currVal= str_replace($tag_close, '</w:t></w:r><w:r><w:t>', $currVal);
}
// handling <u> tag
$el = 'u';
$tag_open = '<' . $el . '>';
$tag_close = '</' . $el . '>';
$nb = substr_count($currVal, $tag_open);
if ( ($nb > 0) && ($nb == substr_count($currVal, $tag_open)) ) {
$currVal= str_replace($tag_open, '</w:t></w:r><w:r><w:rPr><w:u w:val="single"/></w:rPr><w:t>', $currVal);
$currVal= str_replace($tag_close, '</w:t></w:r><w:r><w:t>', $currVal);
}
// handling <br> tag
$el = 'br';
$currVal= str_replace('<br />', '<w:br/>', $currVal);
return $currVal;
}
public function f_handleUnsupportedTags($fieldValue){
$fieldValue = strip_tags($fieldValue, '<b><i><u><br>');
$fieldValue = str_replace(' ',' ',$fieldValue);
$fieldValue = str_replace('<br>','<br />',$fieldValue);
return $fieldValue;
}
この関数を次のように呼び出します。
$fieldVal = $this->f_html2docx($this->f_handleUnsupportedTags($fieldVal));