Last active
April 15, 2022 08:16
-
-
Save sunxyw/61ddc611f41a9007be63359e4172ca5a to your computer and use it in GitHub Desktop.
处理 HTML 中的字符串,并原样输出
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$text = <<<'EOT' | |
<img class="bili-avatar-img" alt="这是ALT里边的文字"> | |
我在外边Outside欸嘿 | |
<span class="bili-avatar-icon bili-avatar-size-60"> | |
这是一串文字欸文字Inside</span> | |
EOT; | |
$no_tags_text = preg_replace('/<.*?>/m', '', $text); | |
preg_match_all('/[\x{4E00}-\x{9FA5}\w_]+/u', $no_tags_text, $matches); | |
$replaced = []; | |
foreach ($matches[0] as $v) { | |
// 这里做处理,$v 是匹配出来的,处理完成后赋值给 $replaced[] 即可 | |
$replaced[] = str_replace('外边', '不是(外边)', $v); | |
} | |
foreach ($replaced as $k => $v) { | |
$text = str_replace($matches[0][$k], $v, $text); | |
} | |
echo $text; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment