W
W
weranda2015-07-08 10:17:16
MODX
weranda, 2015-07-08 10:17:16

Is there a linking module for MODx Revolution in nature?

Greetings.
Is there an extension for MODx for relinking the content of materials, where you could specify the search text and a replacement for it like:
set a word/phrase to search in the material: MODx
set a link for the word/phrase: site.ru
how many times to use on the page: 2
After that, the extension searches the database for the necessary words/phrases and adds a link to them the specified number of times on the page.
I searched the net for something similar, but all I found was homegrown worthless extensions.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Realistic, 2015-07-08
Realistic

There is. Author — Ilya Ershov
Description
Sources

W
weranda, 2015-07-08
@weranda

Here is the code:

<?php
include_once (MODX_ASSETS_PATH .'components/xcontent/xcontent.config.php');

/* @var modX $modx */
/* @var modResource $resource */

//read text
$content = $modx->resource->get('content');
// check config
// $disable = $modx->resource->getTVValue(23);
$disable = false;
if(!isset($REG) || $disable) { print $content; return '';}
$arr = explode("\n", $content);

// Settings
define(DEBUG, 0);
define(SKIP_LINES, 0);
define(LINKS_LIMIT, 2);


if(DEBUG) print "<pre>";
// Начинаем разбор
$links=0;
$str_num=1;
foreach($arr as $str)
{
    $res=0;
    $i=0;

    // Пропускаем строки уже содержащие ссылки или картинки
    $skip=0;
    if(preg_match('/<a.*?>/', $str)) $skip=1;
    if(preg_match('/<img.*?>/', $str)) $skip=1;

    // Также пропускаем первую строку и если лимит ссылок уже достигнут
    if($links < LINKS_LIMIT && !$skip && $str_num>SKIP_LINES)
    {
        foreach($REG as $reg)
        {
            $res = preg_match($reg, $str, $match);
            if($res==0) {  $i++; continue; }

            if(DEBUG) print "\n -------------------- \n(i=$i) "."Str: ".$str."\n";;

            if($res)
            {
                if(DEBUG) print "$links) Found: ".$match[1]."\n";

                // Replace
                //$tag = "<a href='$LINK[$i]'>$match[1]</a>".$match[2];
                $tag = "<a href='$LINK[$i]'>$match[1]</a>";
                $str = preg_replace($REG[$i], $tag, $str, 1);
            }
            $links++;
            if(DEBUG) print "links = $links\n";
            break;
        }
    }

    if(DEBUG) print ">> ";
    //print $str."\n";
    print $str;
    $str_num++;
}
/**/
if(DEBUG) print "</pre>";

I don't know what, where and how to change.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question