PHP code example of freyo / wechat-html2json

1. Go to this page and download the library: Download freyo/wechat-html2json library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

freyo / wechat-html2json example snippets


use Freyo\WeChatMiniProgram\Utils\RichTextParser;

$parsed = RichTextParser::loadHTML($HTML)
    ->setElementNodeHook(function (array $node, \DOMNode $childNode) {
        // remove span node
        if ($childNode->nodeName === 'span') {
            return $node['children'];
        }
        // add width to img node
        if ($childNode->nodeName === 'img') {
            $node['attrs']['width'] = '100%';
        }
        // modify origin DOMElement
        // FOR toHTML() ONLY
        if ($childNode->nodeName === 'a') {
            $childNode->setAttribute('target', '_blank');
        }
        return $node;
    })
    ->setTextNodeHook(function (array $node, \DOMNode $childNode) {
        // remove text node
        if (strpos($childNode->textContent, 'KeyWord') !== false) {
            return null;
        }
        // replace keywords
        $node['text'] = str_replace(
            'keyword', 'KEYWORD', $childNode->textContent
        );
        return $node;
    })
    ->toJSON(); // or toArray() or toHTML()
    
var_dump($parsed);