Running preg_replace in html code is too long

Due to the risk of redirecting to this answer (yes, I read it and spent the last 5 minutes laughing out loud at it), let me explain this problem, which is just one of many.

My employer asked me to look at a site written in PHP using Smarty for templates and MySQL as a DBMS. Currently, it works very slowly, taking up to 2 minutes (with a completely white screen through it all, no less) for a full load.

Profiling the code with xdebug, I found one call to preg_replace, which takes about 30 seconds to complete, which currently goes through all the HTML code and replaces every URL found in its SEO-friendly version. Once it completes, it will output all the code to the browser. (As I said, this is not the only question - the code is quite old and it shows, but I will focus on it for this question.)

Digging further into the code, I found that it is currently scanning 1702 patterns with each matching match (both matches and replacements in arrays of the same size), which certainly takes into account the time it takes.

The code is as follows:

//This is just a call to a MySQL query which gets the relevant SEO-friendly URLs:   
$seourls_data = $oSeoShared->getSeourls();

$url_masks = array();
$seourls = array();
foreach ($seourls_data as $seourl_data)
{
    if ($seourl_data["url"])
    {
        $url_masks[] = "/([\"'\>\s]{1})".$site.str_replace("/", "\/", $seourl_data["url"])."([\#|\"'\s]{1})/";
        $seourls[] = "$1".MAINSITE_URL.$seourl_data["seourl"]."$2";
    }
}

//After filling both $url_masks and $seourls arrays, then the HTML is parsed:
$html_seo = preg_replace($url_masks, $seourls, $html);
//After it completes, $html_seo is simply echo'ed to the browser.

: HTML . , ? , , :

  • (, ) HTML DOMDocument, href .
  • node, URL-, (, , , )
  • ???
  • Profit?

, . ?

.

+4
2

- SEO, URL-, SEO, ...

0

, , , , ,
2 : -
1 - smarty, HTML 2 >
HTMl .

2. , , , , URL- SEO - . SEO ..

www.whatever.com/jobs/722/drupal-php-developer 722 , URL- (drupal-php-developer) , -

3 - ( ) , ( ) .  

0

Source: https://habr.com/ru/post/1537974/


All Articles