Jump to content

preg-replace Unknown Modifier


dmjendor

Recommended Posts

I'm hoping someone here can help, I have recently started setting up docmint to store some documentation online, however I am getting a number of warnings and errors I am trying to work through one by one.  The error that Is currently showing up in my logs is the following:

 

[Tue Oct 28 15:26:06 2008] [error] [client 64.88.170.33] PHP Warning:  preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Unknown modifier '_' in /var/www/docs/engine/modules/Content/lib/func.content.language.items.php on line 218, referer: http://localhost/index.php?id=18

 

The segment of code thats generating it is here:

/**
* returns an array with the content of the page requested, the languages available and the id_tree 
* used to navigate the site
*/
function GetPageContent($id_tree, $lang="", $dbcolumns="content_title,content_language,content_date,content_subtitle,content_author,content_body,date_created,date_edited,id_user_created,id_user_edited,user_created,user_edited,other_module_information", $chapternumbers="default",$published="false")
{
    global $LANG;
    global $TREE;
    global $MY_ENV;
    
    $return = array();
    // check for language
    if ($lang == "") {
        $lang = $MY_ENV['LANG_DEF'];
    }
    
    $id_identifier = TreeConvertTree4IdIdentifier($id_tree);
    // get the result in the temporary array - if there is no LIVE (workflow == 0)
    // item, the array will be empty
    $temppage = ContentGetItemLang($id_identifier, $lang, $dbcolumns, $published);
    if (!empty($temppage)) {
        $return = $temppage;
        // little bonus, get the available languages for each item in the 
        // subarray $PAGE['default_tree']['lang_avail']
        // with the shape of ['lang_id'] = id_content
        $return['lang_avail'] = ContentGetLangAvail($id_identifier);
        // save the id_tree as well that we got in the beginning
        $return['id'] = $id_tree;
        // now check if we need to add the chapter numbering ???
        if (isset ($return['content_title'])) {
            if ($chapternumbers == "default") {
                $chapternumbers = $MY_ENV['chapternumbers'];
            }
            if ($chapternumbers == "true") {
                $return['content_title'] = kill_leading_zeros($TREE['structure'][$id_tree])." ".$return['content_title'];
            }
        }
        // wiki parsing of content_body if set in configure.php
        if (isset ($return['content_body'])) {
            if ($MY_ENV['wysiwyg'] == "wiki") {
                require_once('wiki/class_WikiParser.php');
                // clean the line break: (\n|\f|\r) to ""
                $parser = &new WikiParser();
                $return['content_body'] = $parser->parse($return['content_body']);
                // replace double line breaks from the parser to paragraph breaks
                $patterns = array(
                    "/<br \/><br \/>/", 
                    );
                $replacements = array(
                    "<p>",
                    );
                $return['content_body'] = preg_replace($patterns, $replacements, $return['content_body']);
            }
        }
        // see if we have to build internal links
        if (($MY_ENV['link_internal'] == "true") && isset($return['content_body'])) {
            // use tree['titles'] to match with user language title of articles
            // build search array
            $matcharray = array();
            // build search array for final replacement
            $matcharray2 = array();
            // first escape the reg expr signs
            $regexpr = "/ \ | [ ]";
            $regexpr = explode(" ",$regexpr);
            $regfind = array();
            $regreplace = array();
            foreach ($regexpr as $regitem) {
                array_push($regfind, "/\\".$regitem."/");
                array_push($regreplace, "\\".$regitem);
            }
            // sort the article titles by length to that the replace mechanism will check for
            // long matches first and then the short ones (i.e. find the more complex matches
            // rather than doing the short ones first.
            $temptree = array();
            $counter = 0;    
            foreach ($TREE['title'] as $key=>$value) {
                $counter++;
                $temptree[sprintf ("%03d", strlen($value))."_".$counter]['id_tree'] = $key;
                $temptree[sprintf ("%03d", strlen($value))."_".$counter]['title'] = $value;                
            }
            krsort($temptree);
            if ($MY_ENV['debug'] == "true") {
                print "<pre>";
                print_r($temptree);
                print "</pre>";
            }
            $matchtree = array();
            foreach ($temptree as $treeitem) {
                $matchtree[$treeitem['id_tree']] = $treeitem['title'];
            }
            foreach ($matchtree as $key=>$value) {       
                // replace the reg expr items
                $value = preg_replace($regfind,$regreplace,$value);
                $matchvalue = "/(".htmlspecialchars($MY_ENV['link_internal_chars'])."|".$MY_ENV['link_internal_chars'].")$value(".htmlspecialchars($MY_ENV['link_internal_chars'])."|".$MY_ENV['link_internal_chars'].")/u";
                array_push($matcharray, $matchvalue);
                $newvalue = implode("_",preg_split('//', $value, -1, PREG_SPLIT_NO_EMPTY));
                array_push($matcharray2, "/$newvalue/u");
            }
            // build replace array
            $replacearray = array();
            $replacearray2 = array();
            foreach ($matchtree as $key=>$value) {
                // besides building the link we are also changing all the " " to nbsp; to
                // make sure a subitem is not found at a later routine
                //$newvalue = preg_replace("/\ /"," ",$value);
                //print "$newvalue<br>";
                $newvalue = implode("_",preg_split('//', $value, -1, PREG_SPLIT_NO_EMPTY));
                // check now if we link on the site or in the download
                if (!isset($_REQUEST['mode'])) { // on the site
                    array_push($replacearray, "<a href='".$MY_ENV['BASE_CMS_URL']."/index.php?id=".$key."'>".$newvalue."</a>");
                } else { // in the download document
                    array_push($replacearray, "<a href='#".$TREE['structure'][$key]."'>".$newvalue."</a>");
                }
                array_push($replacearray2, $value);
            }
            // finally, add the chars for replacement if set so they get cleaned out
            // if no match is found in the source code
            if (!empty($MY_ENV['link_internal_chars'])) {
                array_push($matcharray2, "/".$MY_ENV['link_internal_chars']."/u");
                array_push($matcharray2, "/".htmlspecialchars($MY_ENV['link_internal_chars'])."/u");
                array_push($replacearray2, "");
                array_push($replacearray2, "");
            }
            if ($MY_ENV['debug'] == "true") {
                print "<pre>";
                print_r($matcharray);
                print_r($replacearray);
                print_r($matcharray2);
                print_r($replacearray2);
                print "</pre>";
            }
            if (isset ($return['content_body'])) {
                // clean the line break: (\n|\f|\r) to ""
                $return['content_body'] = preg_replace("/(\n|\f|\r)/i","",$return['content_body']);
                $return['content_body'] = preg_replace($matcharray,$replacearray,$return['content_body']);
                $return['content_body'] = preg_replace($matcharray2,$replacearray2,$return['content_body']);
            }
        }
    }
    return $return;
}

 

I have tried replacing the preg_replace with str_replace, but that the regular expressions involved are required as it is filling in the content with underscores and then afterwards removing them again.  THe page renders correctly, but the errors do begin to cause alot of junk to show up in the logs and im sure it must be avoidable.'

 

The line giving the error is the last preg_replace in the code above.

Link to comment
Share on other sites

Some tips:

 

[*]Change your delimiters to avoid multiple escapes

[*]When using literal data inside of a pattern, always escape it using preg_quote

[*]Use character classes in place of one character alternations, e.g., (a|b|c)

 

I suspect the second bullet may alleviate your problem.

Link to comment
Share on other sites

Ok, I placed the $matcharray into a preg_quote using:

$matcharray2 = preg_quote($matcharray2, "/");

 

Which in turn changed the error to:

PHP Warning:  preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Delimiter must not be alphanumeric or backslash in..

 

The content of $matcharray2 looks like this:    [37] => /L_I_N_E___S_W_I_T_C_H/

The content of $replacearray2 looks like this:    [37] => LINE_SWITCH

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.