MyBB Community Forums

Full Version: Find/Replace array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ok I am sick to death of trying to get this find and replace working properly but for the life of me its giving me a headache.

I have tried this:
    //Change something in a 777 chmoded file.
    $fh = fopen('modules/forum/management.php', "r") or die("Could not open file!"); //OPEN FILE
    $size = fread($fh, filesize('modules/forum/management.php')) or die("Could not read file!"); //MAKE TEMPORARY STRING
    fclose($fh); //CLOSE FILE AGAIN
    $fw = fopen('modules/forum/management.php', "r+") or die('Could not open file!'); //OPEN FILE AGAIN
    $data = preg_replace('#'.preg_quote('"defaultsortorder" => $db->escape_string($mybb->input[\'defaultsortorder\']),').'#','"defaultsortorder" => $db->escape_string($mybb->input[\'defaultsortorder\']), 
	"forum_icon" => $db->escape_string($mybb->input[\'forum_icon\']),',$size); //REPLACE IN STRING
	fwrite($fw, $data) or die('Could not write to file');
    $data = preg_replace('#'.preg_quote('$form_container->output_row($lang->forum_link, $lang->forum_link_desc, $form->generate_text_box(\'linkto\', $forum_data[\'linkto\'], array(\'id\' => \'linkto\')), \'linkto\');').'#','$form_container->output_row($lang->forum_link, $lang->forum_link_desc, $form->generate_text_box(\'linkto\', $forum_data[\'linkto\'], array(\'id\' => \'linkto\')), \'linkto\'); 
	$form_container->output_row("Forum Icon", "Add a custom forum icon to appear in place of the default forum icons.", $form->generate_text_box(\'forum_icon\', $forum_data[\'forum_icon\'], array(\'id\' => \'forum_icon\')), \'forum_icon\');',$size); //REPLACE IN STRING
	fwrite($fw, $data) or die('Could not write to file');
    fclose($fw); //CLOSE FILE AGAIN

But with that I get this error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /forums/inc/plugins/pl9forumicons.php on line 181

I have also tried doing 2 fwrite's in one file search but that isn't even working. Having 2 different searches and replacements in one file causes it to only do the first search and replace.

So any help in this are would make my day so much better.

Thanks
$search = array("\"defaultsortorder\" => $db->escape_string($mybb->input['defaultsortorder']),',

What's that at the end of the above? It's either 1:33am kicking in, or I can't figure out what that's for... Toungue
Yeah I already fixed that, I'll fix it above. But I still get the same error. If you know an easier means to find and replace in a file for multiple finds and replaces then please do share. Smile

Trust me, its not the 1:33 am kicking in, you caught something there but I caught it in my file, was just about to update my post when you replied. LOL
Yeah, as many str_replace as you need. Toungue

I think the problem is the $search variable. Although I don't understand any of it - I hate arrays with a passion, so I'm trusting that it is a valid array...!

$search = array("\"defaultsortorder\" => $db->escape_string($mybb->input['defaultsortorder']),", '$form_container->output_row($lang->forum_link, $lang->forum_link_desc, $form->generate_text_box('linkto', $forum_data['linkto'], array('id' => 'linkto')), 'linkto');");

The ' next to the $form_container is causing the problem. Doing this looks like valid code to me (untested):

$search = array("defaultsortorder" => $db->escape_string($mybb->input['defaultsortorder']), $form_container->output_row($lang->forum_link, $lang->forum_link_desc, $form->generate_text_box('linkto'), $forum_data['linkto'], array('id' => 'linkto')), 'linkto');
Tomm M: Thanks for the reply, that actually caused DOUBLE ARROW error. So I took out the code above and replaced it with a non array code that I've been working with. Currently it writes the file and then copies everything and writes it to the end causing it to duplicate alot of the functions which cause it to error out.
Duplicating functions? Doesn't sound too good. What I normally do is to add a prefix; for example I had to change the function import_theme_xml, so I created one called my_import_theme_xml. Might help. Smile
You think this would work instead:

    //Change something in a 777 chmoded file.
    $fh = fopen('modules/forum/management.php', "r") or die("Could not open file!"); //OPEN FILE
    $original = fread($fh, filesize('modules/forum/management.php')) or die("Could not read file!"); //MAKE TEMPORARY STRING
    fclose($fh); //CLOSE FILE AGAIN
    $fw = fopen('modules/forum/management.php', "r+") or die('Could not open file!'); //OPEN FILE AGAIN

    $searchstring[0] = <<<ENDPHP
"defaultsortorder" => \$db->escape_string(\$mybb->input['defaultsortorder']),
ENDPHP;
    $pattern[0]'#'.preg_quote($searchstring[0]).'#';
    $replacement[0] = <<<ENDPHP
"defaultsortorder" => \$db->escape_string(\$mybb->input['defaultsortorder']), 
    "forum_icon" => \$db->escape_string(\$mybb->input['forum_icon']),
ENDPHP;

    $searchstring[1] = <<<ENDPHP
\$form_container->output_row(\$lang->forum_link, \$lang->forum_link_desc, \$form->generate_text_box('linkto', \$forum_data['linkto'], array('id' => 'linkto')), 'linkto');
ENDPHP;
    $pattern[1]'#'.preg_quote($searchstring[1]).'#';
    $replacement[1] = <<<ENDPHP
\$form_container->output_row(\$lang->forum_link, \$lang->forum_link_desc, \$form->generate_text_box('linkto', \$forum_data['linkto'], array('id' => 'linkto')), 'linkto');
    \$form_container->output_row("Forum Icon", "Add a custom forum icon to appear in place of the default forum icons.",
    \$form->generate_text_box('forum_icon', \$forum_data['forum_icon'], array('id' => 'forum_icon')), 'forum_icon');
ENDPHP;

    $modified = preg_replace($pattern, $replacement, $original); //REPLACE IN STRING
    fwrite($fw, $modified) or die('Could not write to file');
    fclose($fw); //CLOSE FILE AGAIN 

Haven't tested it yet and it was provided by someone else I'm just afraid to test it right now. LOL