tags after
if( trim( $content )=='' ){ $start = pq('h1:first'); $nextElements = pq($start)->nextAll(); foreach ($nextElements as $element) { if (pq($element)->is('p')) { $start = trim(html_entity_decode(pq($element)->text())); } } foreach ( pq( 'p' ) as $p ) { $content .= " " . trim( html_entity_decode( pq( $p )->text() ) ) . " "; } if( trim( $start )!='' ) $content = strstr( $content, $start ); $color ='#000080'; } // Extracting from
tags if( trim( $content )=='' ){ foreach ( pq( 'p' ) as $p ) { $content .= " " . trim( html_entity_decode( pq( $p )->text() ) ) ; } $color ='#888'; } // Adding a space after punctuation and various deletions $content = str_replace(array("\r", "\n"), ' ', $content); $remove = [ '?', '!', ";", "!", "»", "]",]; foreach ( $remove as $phrase ) { $content = str_replace( $phrase, $phrase . ' ', $content ); } $content = str_replace( html_entity_decode(' '), " ", $content ); // Standardizing spaces $content = preg_replace( '/\s{2,}/', ' ', $content ); // one space, no more $pattern = '/À lire aussi.{1,200}?»/'; $content = preg_replace($pattern, ' ', $content); $pattern = '/Temps de Lecture.{1,20}? Fiche/'; $content = preg_replace($pattern, ' ', $content); $pattern = '/Temps de Lecture.{1,20}? min./'; $content = preg_replace($pattern, ' ', $content); // Media-specific cutting rules if( substr_count( $url, ".elle.fr" ) ){ $pattern = '/Note :.{13,45}? sur 5/'; $content = preg_replace($pattern, ' ', $content); $content = str_replace( strstr( $content, " sur 5 " ), '', $content ); } if( substr_count( $url, ".latribune.fr" ) ){ $content = str_replace( "Partager :", ' ', $content ); } if( substr_count( $url, ".techno-science.net" ) ){ $content = strip_tags( strstr( $htmlContent, "suivez-nous sur" ) ); $content = strstr( $content, ');' ); $content = html_entity_decode( str_replace( strstr( $content, "Référence" ), "", $content ) ); } if( substr_count( $url, ".lexpress.fr" ) ){ $content = strstr( $htmlContent, "article__text" ) ; $content = strstr( $content, ">" ); $content = substr( $content,1,99999 ); $content = html_entity_decode( strip_tags( str_replace( strstr( $content, "
" . "Title: $title
" . "Content: $content"; if( strlen( $content ) != strlen( $content_or ) ){ echo "
$content_or"; // original content } // Formatting the output content for the txt file in the $add variable $content = strip_tags($content); $add = html_entity_decode( $title ) . '. '; if( trim( $txt_clean ) != '' ) $add .= str_replace( "..", ".", html_entity_decode( $txt_clean ) . '. ' ); if( strlen( $content ) > 160 ) $add .= $content . '.'; $add = trim( clean_text( $add ) ); $remove = [ '. .', '..', "?.", "!.", ";.", "??", "? ?",]; foreach ( $remove as $phrase ) { $add = str_replace( $phrase, substr( $phrase,0,1 ). ' ', $add ); } $add = strip_tags( $add ); $add = str_replace( html_entity_decode( ' ' ), " ", $add ); $add = preg_replace( '/(x[0-9a-fA-F]+|\d+);/', ' ', $add ); $add = preg_replace( '/\d+;/', ' ', $add ); $add = preg_replace( '/[\x{1F534}\x{26A0}\x{1F3C9}\x{1F6A8}\x{1F6D2}\x{1FA82}\x{25B6}\x{2139}\x{2600}-\x{26FF}\x{1F300}-\x{1F5FF}\x{1F600}-\x{1F64F}\x{1F680}-\x{1F6FF}\x{1F700}-\x{1F77F}\x{1F780}-\x{1F7FF}\x{1F800}-\x{1F8FF}\x{1F900}-\x{1F9FF}\x{1FA00}-\x{1FA6F}\x{1FA70}-\x{1FAFF}]/u', ' ', $add ); $add = preg_replace( '/\s{2,}/', ' ', $add ); $replacements = array( "À" => "À", "Â" => "Â", "Ã" => "Ã", "Ä" => "Ä", "Â" => "Â", "Ã…" => "Å", "á" => "á", "â" => "â", "ã" => "ã", "ä" => "ä", "Ã¥" => "å", "Ã’" => "Ò", "Ó" => "Ó", "Ô" => "Ô", "Õ" => "Õ", "Ö" => "Ö", "Ø" => "Ø", "ò" => "ò", "ó" => "ó", "ô" => "ô", "õ" => "õ", "ö" => "ö", "ø" => "ø", "È" => "È", "É" => "É", "Ê" => "Ê", "Ë" => "Ë", "è" => "è", "é" => "é", "ê" => "ê", "ë" => "ë", "Ç" => "Ç", "ç" => "ç", "ÃŒ" => "Ì", "ÃŽ" => "Î", "ì" => "ì", "Ã" => "í", "î" => "î", "ï" => "ï", "Ù" => "Ù", "Ú" => "Ú", "Û" => "Û", "Ãœ" => "Ü", "ù" => "ù", "ú" => "ú", "û" => "û", "ü" => "ü", "ÿ" => "ÿ", "Ñ" => "Ñ", "ñ" => "ñ", "Ã" => "Á", "Ã" => "Í", "à " => "à", '’'=>'\'', "«" => "«", '»'=>'»'); $add = str_replace( array_keys( $replacements ), array_values( $replacements ), $add ); // Additional display for debugging and testing if( $debug ){ if( trim( $content )=='' ) { echo "
html:
$html " . htmlentities( $html ) . "
" . htmlentities( $doc->text() ) .'
' . 'Source lenght: ' . strlen( $htmlContent ); } $test_char =""; foreach ( str_split( $add ) as $char) { $test_char .= $char . "(" . bin2hex($char). ") "; } echo '
hexadecimal code for each character from $add var:
' . $test_char; } else { if( $id_source%10000 === 0 ) mysqli_query( $mysqli, "OPTIMIZE TABLE `base_news`;" ); } // Saving the title + description + text in a text file $key_title = md5( $title ); $nb_base = mysqli_return_number( $mysqli, "SELECT `id` FROM `base_news` WHERE `key_title`='$key_title' LIMIT 1" ); if( isset( $_GET["id"] ) OR isset( $_GET["media"] ) OR ( strlen( $content ) > 200 AND !$nb_base ) ){ mysqli_query( $mysqli, "UPDATE `base_news` SET `key_title` = '$key_title' WHERE `id`='$id_source' LIMIT 1" ); $bom = "\xEF\xBB\xBF"; file_put_contents( "$path/sources/txt_news/$id_source.txt", $bom . $add ); } $execution_time = microtime(true) - $time_start; echo"
execution time: $execution_time"; if( $id_source%100 === 0 ) sleep(1); // throttling navigation to prevent the browser from hanging. if( $redirect_enabled ) echo ""; ?>