没有不值得去解决的问题,也没有不值得去学习的技术!

How to Detect Specific HTML Tags in PHP

When the content only contains text such as , and audio, rather than closed and complete HTML tags, the regex match is not triggered.

1. When publishing an article to WeChat, WeChat responded with a failure: error code: 45166, error message: invalid content hint: [pzRHda0286d228] rid: 61041ffe-5cefd269-6c9611ff.

2. The final analysis concluded that this was caused by the inclusion of the tag: mpvoice in the article content. The article content is as follows:

Plaintext
内容contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent

3. I finally decided to check whether the HTML contains the tag: mpvoice before calling the WeChat API. If it exists, the subsequent process is skipped to save the user’s waiting time.

4. The final implementation code is as follows:

PHP
$mpvoicePattern = "/]+)>(?:(?!<\/mpvoice>)[\s\S])*<\/mpvoice>/s";
$audioPattern = "/]+)>(?:(?!<\/audio>)[\s\S])*<\/audio>/s";
preg_match($mpvoicePattern, $item['content'], $mpvoiceMatches);
preg_match($audioPattern, $item['content'], $audioMatches);
if (!empty($mpvoiceMatches)) {
    $this->addError($attribute, Yii::t('error', Yii::t('error', Yii::t('error', '202247'), ['tag' => 'mpvoice'])));
    break;
}
if (!empty($audioMatches)) {
    $this->addError($attribute, Yii::t('error', Yii::t('error', Yii::t('error', '202247'), ['tag' => 'audio'])));
    break;
}

5. The results of printing $mpvoiceMatches and $audioMatches respectively are as follows:

PHP
内容contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentListen to the T-Rex:Your browser does not support theaudio element.

Plaintext
Array
(
    [0] => 
    [1] =>  class="js_editor_audio audio_iframe js_uneditable" src="/cgi-bin/readtemplate?t=tmpl/audio_tmpl&amp;name=%E4%B8%80%E4%B8%AA%E4%BA%BA%EF%BC%8C%E4%B9%9F%E6%8C%BA%E5%A5%BD&amp;play_length=05:22" isaac2="1" low_size="612.81" source_size="612.8" high_size="2524.76" name="一个人,也挺好" play_length="322000" voice_encode_fileid="MzI1NjA0MDg2M18yNjUwNzYzNTcz" data-topic_id="1693506805106065417" data-topic_name="夜听精选" data-pluginname="insertaudio" style="margin-top: 50px; margin-right: 0px; margin-bottom: 0px; max-width: 100%;"
)
Array
(
    [0] => Your browser does not support theaudio element.
    [1] =>  controls src="/media/cc0-audio/t-rex-roar.mp3"
)

6. When the content only contains text such as <mpvoice, /mpvoice>, and audio, rather than closed and complete HTML tags, the regex match is not triggered. See Figure 1.

When the content only contains text such as <mpvoice, /mpvoice>, and audio, rather than closed and complete HTML tags, the regex match is not triggered.
Figure 1
Plaintext
内容contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent/mpvoice>Listen to the T-Rex:audio controls src=\"/media/cc0-audio/t-rex-roar.mp3\">Your browser does not support theaudio element.

Plaintext
Array
(
    [0] => 
    [1] =>  class="js_editor_audio audio_iframe js_uneditable" src="/cgi-bin/readtemplate?t=tmpl/audio_tmpl&amp;name=%E4%B8%80%E4%B8%AA%E4%BA%BA%EF%BC%8C%E4%B9%9F%E6%8C%BA%E5%A5%BD&amp;play_length=05:22" isaac2="1" low_size="612.81" source_size="612.8" high_size="2524.76" name="一个人,也挺好" play_length="322000" voice_encode_fileid="MzI1NjA0MDg2M18yNjUwNzYzNTcz" data-topic_id="1693506805106065417" data-topic_name="夜听精选" data-pluginname="insertaudio" style="margin-top: 50px; margin-right: 0px; margin-bottom: 0px; max-width: 100%;"
)
Array
(
    [0] => Your browser does not support theaudio element.
    [1] =>  controls src="/media/cc0-audio/t-rex-roar.mp3"
)

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系