Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Commit

Permalink
Refactor - $modx->getTagsFromContent()
Browse files Browse the repository at this point in the history
Speed up!!!
  • Loading branch information
yama committed Feb 15, 2015
1 parent f4225b4 commit 1cebc29
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions manager/includes/document.parser.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,56 +780,54 @@ function getTagsFromContent($content,$left='[+',$right='+]') {
}

function _getTagsFromContent($content, $left='[+',$right='+]') {
$_tmp = $content;
$spacer = md5('dummy');
if(strpos($_tmp,']]>')!==false) $_tmp = str_replace(']]>', "]{$spacer}]>",$_tmp);
if(strpos($_tmp,';}}')!==false) $_tmp = str_replace(';}}', ";}{$spacer}}",$_tmp);
if(strpos($_tmp,'{{}}')!==false) $_tmp = str_replace('{{}}',"{{$spacer}{}{$spacer}}",$_tmp);
$count_left = 0;
$count_right = 0;
if(strpos($content,$left)===false) return array();
$spacer = md5('<<<MODX>>>');
if(strpos($content,']]>')!==false) $content = str_replace(']]>', "]{$spacer}]>",$content);
if(strpos($content,';}}')!==false) $content = str_replace(';}}', ";}{$spacer}}",$content);
if(strpos($content,'{{}}')!==false) $content = str_replace('{{}}',"{{$spacer}{}{$spacer}}",$content);
$_ = explode($left,$content);
$piece = array();
$c_ = count($_)-1;
foreach($_ as $i=>$v) {
if($i===0) continue;
$piece[] = $left;
if(strpos($v,$right)===false) $piece[] = $v;
else {
$__ = explode($right,$v);
$c__ = count($__)-1;
foreach($__ as $i=>$v) {
$piece[] = $v;
if($i<$c__) $piece[] = $right;
}
}
}

$strlen_left = strlen($left);
$strlen_right = strlen($right);
$key = '';
$c = 0;
while($_tmp!=='')
{
$bt = $_tmp;
$key .= substr($_tmp,0,1);
$_tmp = substr($_tmp,1);
$strpos_left = strpos($key,$left);
if($strpos_left!==false && substr($key,-$strlen_right)===$right)
{
$key = substr($key,$strpos_left);
if(substr_count($key,$left)===substr_count($key,$right))
{
foreach($piece as $v) {
$key .= $v;
if(substr($key,-$strlen_right)===$right) {
if(substr_count($key,$left)===substr_count($key,$right)) {
$key = substr($key, (strpos($key,$left) + $strlen_left) );
$tags[] = substr($key,0,strlen($key)-$strlen_right);
$key = '';
}
}
if($bt === $_tmp) break;
if(1000000<$c) exit('Fetch tags error');
$c++;
}
if(!$tags) return array();

foreach($tags as $tag)
{
if(strpos($tag,$left)!==false)
{
foreach($tags as $tag) {
if(strpos($tag,$left)!==false) {
$fetch = $this->_getTagsFromContent($tag,$left,$right);
foreach($fetch as $v)
{
foreach($fetch as $v) {
$tags[] = $v;
}
}
}
$i = 0;
foreach($tags as $tag)
{
if(strpos($tag,"]{$spacer}]>")!==false) $tags[$i] = str_replace("]{$spacer}]>", ']]>', $tag);
if(strpos($tag,";}{$spacer}}")!==false) $tags[$i] = str_replace(";}{$spacer}}", ';}}', $tag);
if(strpos($tag,"{{$spacer}{}{$spacer}}")!==false) $tags[$i] = str_replace("{{$spacer}{}{$spacer}}", '{{}}',$tag);
foreach($tags as $tag) {
if(strpos($tag,"$spacer")!==false) $tags[$i] = str_replace("$spacer", '', $tag);
$i++;
}
return $tags;
Expand Down

0 comments on commit 1cebc29

Please sign in to comment.