1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
| <?php
echo "=== 文本处理和内容提取 ===\n";
class TextProcessor { public static function extractSummary($content, $maxLength = 200) { $text = strip_tags($content); $text = preg_replace('/\s+/', ' ', trim($text)); if (mb_strlen($text) <= $maxLength) { return $text; } $summary = mb_substr($text, 0, $maxLength); $lastSpace = mb_strrpos($summary, ' '); if ($lastSpace !== false) { $summary = mb_substr($summary, 0, $lastSpace); } return $summary . '...'; } public static function extractKeywords($text, $minLength = 3, $maxCount = 10) { $text = strtolower($text); $text = preg_replace('/[^\w\s\x{4e00}-\x{9fa5}]/u', ' ', $text); $words = preg_split('/\s+/', $text, -1, PREG_SPLIT_NO_EMPTY); $stopWords = ['的', '是', '在', '有', '和', '与', '或', '但', '而', '了', '着', '过', 'the', 'is', 'at', 'which', 'on', 'and', 'or', 'but']; $filteredWords = array_filter($words, function($word) use ($minLength, $stopWords) { return mb_strlen($word) >= $minLength && !in_array($word, $stopWords); }); $wordCount = array_count_values($filteredWords); arsort($wordCount); return array_slice(array_keys($wordCount), 0, $maxCount); } public static function detectLanguage($text) { $patterns = [ 'chinese' => '/[\x{4e00}-\x{9fa5}]/u', 'english' => '/[a-zA-Z]/', 'japanese' => '/[\x{3040}-\x{309f}\x{30a0}-\x{30ff}]/u', 'korean' => '/[\x{ac00}-\x{d7af}]/u', 'arabic' => '/[\x{0600}-\x{06ff}]/u', 'russian' => '/[\x{0400}-\x{04ff}]/u' ]; $scores = []; foreach ($patterns as $lang => $pattern) { preg_match_all($pattern, $text, $matches); $scores[$lang] = count($matches[0]); } arsort($scores); $topLang = key($scores); return [ 'language' => $topLang, 'confidence' => $scores[$topLang] / mb_strlen($text), 'scores' => $scores ]; } public static function extractContacts($text) { $contacts = [ 'emails' => [], 'phones' => [], 'urls' => [] ]; $emailPattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/'; if (preg_match_all($emailPattern, $text, $matches)) { $contacts['emails'] = array_unique($matches[0]); } $phonePatterns = [ '/1[3-9]\d{9}/', '/\d{3}-\d{4}-\d{4}/', '/\(\d{3}\)\s*\d{4}-\d{4}/', '/\d{4}-\d{7}/' ]; foreach ($phonePatterns as $pattern) { if (preg_match_all($pattern, $text, $matches)) { $contacts['phones'] = array_merge($contacts['phones'], $matches[0]); } } $contacts['phones'] = array_unique($contacts['phones']); $urlPattern = '/https?:\/\/[^\s\/$.?#].[^\s]*/'; if (preg_match_all($urlPattern, $text, $matches)) { $contacts['urls'] = array_unique($matches[0]); } return $contacts; } public static function formatText($text, $options = []) { $defaults = [ 'remove_extra_spaces' => true, 'fix_punctuation' => true, 'capitalize_sentences' => true, 'remove_empty_lines' => true ]; $options = array_merge($defaults, $options); if ($options['remove_extra_spaces']) { $text = preg_replace('/\s+/', ' ', $text); } if ($options['fix_punctuation']) { $text = preg_replace('/\s+([,.!?;:])/', '$1', $text); $text = preg_replace('/([,.!?;:])\s*/', '$1 ', $text); } if ($options['capitalize_sentences']) { $text = preg_replace_callback('/([.!?]\s+)([a-z])/', function($matches) { return $matches[1] . strtoupper($matches[2]); }, $text); $text = ucfirst(trim($text)); } if ($options['remove_empty_lines']) { $text = preg_replace('/\n\s*\n/', "\n", $text); } return trim($text); } public static function getTextStats($text) { $stats = [ 'characters' => mb_strlen($text), 'characters_no_spaces' => mb_strlen(preg_replace('/\s/', '', $text)), 'words' => str_word_count(strip_tags($text)), 'sentences' => preg_match_all('/[.!?]+/', $text), 'paragraphs' => preg_match_all('/\n\s*\n/', $text) + 1, 'lines' => substr_count($text, "\n") + 1 ]; $stats['reading_time'] = ceil($stats['words'] / 200); $stats['avg_sentence_length'] = $stats['sentences'] > 0 ? round($stats['words'] / $stats['sentences'], 1) : 0; return $stats; } }
echo "=== 文本处理器示例 ===\n";
$sampleText = "这是一篇关于PHP正则表达式的文章。正则表达式是处理文本的强大工具,在数据验证、文本解析和内容提取中发挥着重要作用。联系我们:邮箱support@example.com,电话13812345678,网站https://www.example.com。";
$summary = TextProcessor::extractSummary($sampleText, 50); echo "文章摘要: $summary\n";
$keywords = TextProcessor::extractKeywords($sampleText); echo "关键词: " . implode(', ', $keywords) . "\n";
$langInfo = TextProcessor::detectLanguage($sampleText); echo "检测语言: {$langInfo['language']} (置信度: " . round($langInfo['confidence'], 2) . ")\n";
$contacts = TextProcessor::extractContacts($sampleText); echo "联系信息:\n"; echo "- 邮箱: " . implode(', ', $contacts['emails']) . "\n"; echo "- 电话: " . implode(', ', $contacts['phones']) . "\n"; echo "- 网址: " . implode(', ', $contacts['urls']) . "\n";
$stats = TextProcessor::getTextStats($sampleText); echo "\n文本统计:\n"; foreach ($stats as $key => $value) { $label = [ 'characters' => '字符数', 'characters_no_spaces' => '字符数(不含空格)', 'words' => '单词数', 'sentences' => '句子数', 'paragraphs' => '段落数', 'lines' => '行数', 'reading_time' => '阅读时间(分钟)', 'avg_sentence_length' => '平均句长' ][$key] ?? $key; echo "- $label: $value\n"; }
$messyText = "这是一段 格式混乱的文本 。有多余的空格 ,标点符号也不规范,还有 。"; $formattedText = TextProcessor::formatText($messyText); echo "\n格式化前: $messyText\n"; echo "格式化后: $formattedText\n"; ?>
|