За последние 24 часа нас посетили 23087 программистов и 1225 роботов. Сейчас ищет 741 программист ...

Помогите оптимизировать регулярку

Тема в разделе "Регулярные выражения", создана пользователем xShiftx, 27 окт 2018.

  1. xShiftx

    xShiftx Новичок

    С нами с:
    27 май 2018
    Сообщения:
    25
    Симпатии:
    0
    PHP:
    1. <?php
    2.  
    3. /**
    4.   *
    5.   * Hidden Statistics for external resources
    6.   * .........................version 1.2.2
    7.   *
    8.   * Developer: CyberX
    9.   * License: MIT
    10.   *
    11.   */
    12.  
    13. // Main config
    14. require_once('./core/struct/DataBase.php');
    15. require_once('./private/config.php');
    16.  
    17. // Structures
    18. require_once('./core/libraries/DBX.php');
    19. require_once('./core/libraries/Cipher.php');
    20. require_once('./core/libraries/SafeHTML.php');
    21. require_once('./private/config.php');
    22.  
    23. $dbConfig = file_get_contents($_SERVER["DOCUMENT_ROOT"] .'/private/db_config.ini', true);
    24.  
    25. /* Check installation */
    26. if( strlen($dbConfig) > 0 ) {
    27.  
    28.   $cipher = new Cipher();
    29.   $safe = new SafeHTML();
    30.    
    31.   $dbx_data = explode('|', $cipher::crypt('decrypt', $dbConfig));
    32.  
    33.   // get database engine API
    34.   $dbx = new DBX($dbx_data);
    35.  
    36.   $query = [];
    37.  
    38.   if( !empty($_GET['query']) ) {
    39.  
    40.       $query_string = $safe::safe($_GET['query']);
    41.  
    42.     // QUERY PATTERN FIELDS
    43.     $query['field_route']['value'] = '';
    44.     $query['field_title']['value'] = '';
    45.     $query['field_description']['value'] = '';
    46.     $query['field_content']['value'] = $query_string;
    47.  
    48.     $dbx::query('q', 'revolver__nodes', $query);
    49.  
    50.     $search_query_result = $dbx::$result['result'];
    51.     $output = '<div class="revolver__search_results"><ul>';
    52.  
    53.     foreach ($search_query_result as $k => $v) {
    54.      
    55.       $output .= '<li><a href="'. $v['field_route'] .'" title="'. $v['field_description'] .'">'. $v['field_title'] .'</a><span>'. $v['field_description'] .'</span>';
    56.  
    57.       $replace = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(strip_tags(html_entity_decode(strip_tags(preg_replace('/<[^>]*>/','',str_replace(array("&nbsp;","\n","\r"),"",html_entity_decode($v['field_content'], ENT_QUOTES,'UTF-8'))))))))));
    58.      
    59.       $snippet = preg_split("/". $query_string ."/i", $replace);
    60.  
    61.       $counter = 1;
    62.  
    63.       foreach ($snippet as $snip) {
    64.  
    65.           $length = ( (int)strlen( $snip ) / 100) * 30;
    66.  
    67.           if( $counter % 2 !== 0 ) {
    68.  
    69.               $highlight_1 = substr($snip, $length, 0);  
    70.  
    71.           }
    72.           else {
    73.  
    74.             $highlight_2 = substr($snip, 0, $length);
    75.          
    76.           }
    77.  
    78.           $counter++;
    79.  
    80.       }
    81.  
    82.       $output .= '<dfn style="display:block; margin-bottom: 10px;">... '. $highlight_1 . $query_string . $highlight_2 .' ...</dfn></li>';
    83.  
    84.     }
    85.  
    86.     $output .= '</ul><p>Search for <b>'. $query_string .'</b>.</p></div>';
    87.  
    88.     print $output;
    89.  
    90.  
    91.   }
    92.   else {
    93.  
    94.     print '<div class="revolver__search_results"><p>Empty query</p></div>';
    95.  
    96.   }
    97.  
    98. }
    99.  
    100. ?>
    Конкретно вот здесь приходится два раза делать одно и то же(почему так?).

    PHP:
    1. $replace = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(strip_tags(html_entity_decode(strip_tags(preg_replace('/<[^>]*>/','',str_replace(array("&nbsp;","\n","\r"),"",html_entity_decode($v['field_content'], ENT_QUOTES,'UTF-8'))))))))));
    спасибо!