$text, 'original' => $original, 'flags' => $flags, ); } /** * Stage 1: replace typography characters with ASCII equivalents. * No flag is added — these are tidy-ups the user doesn't need to see. * * @param string $text Input. * @return string Cleaned text. */ private static function strip_smart_chars( $text ) { // Handle em dash with surrounding spaces: " — " becomes ", " $text = preg_replace( '/ \xE2\x80\x94 /', ', ', $text ); // General character replacements for other typography chars. $replacements = array( "\xE2\x80\x94" => ',', // em dash (U+2014) — fallback for edge cases "\xE2\x80\x93" => '-', // en dash (U+2013) "\xE2\x80\x98" => "'", // left single quote (U+2018) "\xE2\x80\x99" => "'", // right single quote (U+2019) "\xE2\x80\x9C" => '"', // left double quote (U+201C) "\xE2\x80\x9D" => '"', // right double quote (U+201D) "\xE2\x80\xA6" => '...', // ellipsis (U+2026) "\xC2\xA0" => ' ', // non-breaking space (U+00A0) ); return strtr( $text, $replacements ); } /** * Stage 2: replace AI-coded marketing phrases with neutral wording, * or strip outright. Each replacement adds a flag so the UI can show * the change to the user. * * @param string $text Input. * @return array { 0: cleaned string, 1: flags array } */ private static function strip_tier_a( $text ) { $rules = array( // [ pattern, replacement, note ] // .{0,40} not [\w\s]{0,40}: hyphens (e.g. "fast-paced") aren't word/whitespace chars. array( "/in today's .{0,40}(world|landscape|environment),?\\s*/i", '', '"in today\'s..." opener removed (AI marketing copy)' ), array( '/\bdelve into\b/i', '$EXPLORE$', '"delve into" replaced with "explore"' ), array( '/\bnavigate the landscape\b/i', 'work in this area', '"navigate the landscape" replaced' ), array( '/\bunlock the potential\b/i', 'use', '"unlock the potential" replaced with "use"' ), array( '/\bleverage\b/i', 'use', '"leverage" replaced with "use"' ), array( '/\bseamlessly\b/i', '', '"seamlessly" removed (AI marketing copy)' ), array( '/\bseamless\b/i', '', '"seamless" removed (AI marketing copy)' ), array( '/\brobust\b/i', '', '"robust" removed (AI marketing copy)' ), array( '/\belevate\b/i', '$IMPROVE$', '"elevate" replaced with "improve"' ), array( '/\btapestry\b/i', '', '"tapestry" removed (AI marketing copy)' ), array( '/\btreasure trove\b/i', '', '"treasure trove" removed (AI marketing copy)' ), array( '/\bvibrant\b/i', '', '"vibrant" removed (AI marketing copy)' ), array( '/\bbustling\b/i', '', '"bustling" removed (AI marketing copy)' ), ); $flags = array(); foreach ( $rules as $rule ) { list( $pattern, $replacement, $note ) = $rule; $match_count = preg_match_all( $pattern, $text ); if ( $match_count > 0 ) { $text = preg_replace( $pattern, $replacement, $text ); $flags[] = array( 'type' => 'banned_word', 'pattern' => $pattern, 'severity' => 'auto-fixed', 'note' => $note, 'count' => $match_count, ); } } // Resolve placeholders preserving case of first letter where possible. $text = self::resolve_case_placeholders( $text ); // Tidy double spaces and leading punctuation introduced by strips. $text = preg_replace( '/ +/', ' ', $text ); $text = preg_replace( '/^[,\s]+/', '', $text ); $text = preg_replace( '/\s+([.,;:!?])/', '$1', $text ); // Collapse trailing punctuation pile-ups like ",." or "; ." into the closer. $text = preg_replace( '/([,;:])\s*([.!?])/', '$2', $text ); return array( $text, $flags ); } /** * Replace $EXPLORE$ and $IMPROVE$ placeholders with the right case * based on the surrounding sentence (capitalised at sentence start, * lower-case otherwise). * * @param string $text Input with placeholders. * @return string */ private static function resolve_case_placeholders( $text ) { $words = array( '$EXPLORE$' => 'explore', '$IMPROVE$' => 'improve' ); foreach ( $words as $placeholder => $word ) { // Capitalised if at start of string or after sentence-ending punctuation. $text = preg_replace_callback( '/(^|[.!?]\s+)' . preg_quote( $placeholder, '/' ) . '/', function ( $m ) use ( $word ) { return $m[1] . ucfirst( $word ); }, $text ); $text = str_replace( $placeholder, $word, $text ); } return $text; } /** * Stage 3a: strip "Look," / "Listen," sentence openers. * * @param string $text Input. * @return array { 0: cleaned, 1: flags } */ private static function strip_tier_b_openers( $text ) { $flags = array(); if ( preg_match( '/^(Look|Listen),\s+/i', $text ) ) { $text = preg_replace( '/^(Look|Listen),\s+/i', '', $text ); $flags[] = array( 'type' => 'opener', 'severity' => 'auto-fixed', 'note' => 'Removed "Look," / "Listen," opener (AI conversational tic)', ); } return array( $text, $flags ); } /** * Stage 3b: detect "X or Y" rhetorical patterns and "...and beyond" closers. * Does not modify text — flags only. * * @param string $text Input. * @return array Flags (possibly empty). */ private static function detect_tier_b_patterns( $text ) { $patterns = array( array( 'regex' => '/it\'s not just [^.]+,\s*it\'s\b/i', 'note' => 'Uses "It\'s not just X, it\'s Y" construction common in AI copy. Edit before saving.', ), array( 'regex' => '/whether you\'re an? [^.]+,?\s*or an? \b/i', 'note' => 'Uses "Whether you\'re a X or a Y" construction common in AI copy. Edit before saving.', ), array( 'regex' => '/\b(and|going) beyond\.?\s*$/i', 'note' => 'Ends with "...and beyond" — AI rhetorical closer. Edit before saving.', ), array( 'regex' => '/\.\.\.and beyond/i', 'note' => 'Contains "...and beyond" — AI rhetorical closer. Edit before saving.', ), ); $flags = array(); foreach ( $patterns as $p ) { if ( preg_match( $p['regex'], $text ) ) { $flags[] = array( 'type' => 'sentence_pattern', 'severity' => 'needs-review', 'note' => $p['note'], ); } } return $flags; } }