Hi and I have the below php-gtk script but for some reason it is reporting the error "Not enough storage is available to process this command." in the command console but the application still loads. It only happens when I try to import a 1.75MB text file for processing. Does anybody know why regex and str replace on a 1.75MB file causes this error or how to increase its storage. Please advice.

<?php
ini_set('memory_limit','256M');
if (!class_exists('gtk')) {
    die("Please load the php-gtk2 module in your php.ini\r\n");
}
function func_generate() {
//reword button clicked.
return true;
}
function func_open() {
global $wnd, $filebox, $textareaBuffer,$text;
$fileselection = new GtkFileSelection('Open');
$fileselection->set_select_multiple(false);
$fileselection->complete('*.*');
$fileselection->hide_fileop_buttons();
$fileselection->show_all();
$fileselection->cancel_button->hide_all();
//$fileselection->history_pulldown->hide_all();
if ($fileselection->run() == Gtk::RESPONSE_OK) {
    if (@is_readable($fileselection->get_filename())) {
        $fileselection->hide_all();
        $filebox->set_text($fileselection->get_filename());
        $text=str_replace(array('(',')','  ','  '),array(' ( ',' ) ',' ',' '),preg_replace('#[^a-zA-Z0-9\'\(\) ]#',' ',file_get_contents($fileselection->get_filename())));
        /*$text_tmp=file_get_contents($fileselection->get_filename());
        $text_tmp=str_split($text_tmp,1);
        $arr=array();
        $chars=array('a'=>true,'b'=>true,'c'=>true,'d'=>true,'e'=>true,'f'=>true,'g'=>true,'h'=>true,'i'=>true,'j'=>true,'k'=>true,'l'=>true,'m'=>true,'n'=>true,'o'=>true,'p'=>true,'q'=>true,'r'=>true,'s'=>true,'t'=>true,'u'=>true,'v'=>true,'w'=>true,'x'=>true,'y'=>true,'z'=>true,'A'=>true,'B'=>true,'C'=>true,'D'=>true,'E'=>true,'F'=>true,'G'=>true,'H'=>true,'I'=>true,'J'=>true,'K'=>true,'L'=>true,'M'=>true,'N'=>true,'O'=>true,'P'=>true,'Q'=>true,'R'=>true,'S'=>true,'T'=>true,'U'=>true,'V'=>true,'W'=>true,'X'=>true,'Y'=>true,'Z'=>true,'('=>true,')'=>true,'\''=>true,' '=>true);
        for ($i=0;$i<count($text_tmp);$i++) {
            if (isset($chars[$text_tmp[$i]])) {
                if (($text_tmp[$i]=='(' || $text_tmp[$i]==')') && $text_tmp[($i-1)]!=' ') {
                    $arr[]=' ';
                    }
                $arr[]=$text_tmp[$i];
                if (($text_tmp[$i]=='(' || $text_tmp[$i]==')') && $text_tmp[($i+1)]!=' ') {
                    $arr[]=' ';
                    }
                }
            }
        unset($text_tmp);
        $text='';
        foreach($arr AS $val) {
        $text.=$val;
        }*/
        $textareaBuffer->set_text($text);
        } else {
        self::open();
        }
    }
return true;
}
 
$wnd = new GtkWindow();
$wnd->resize(875,700);
$wnd->set_title('Reworder bot - by cwarn23');
$wnd->connect_simple('destroy', array('gtk', 'main_quit'));
 

// Create a new buffer and a new view to show the buffer.
$textareaBuffer = new GtkTextBuffer();
$textarea       = new GtkTextView();
 
// Add some text to the buffer.
$textareaBuffer->set_text('');
 
// Add the buffer to the view and make sure no one edits the text.
$textarea->set_buffer($textareaBuffer);
$textarea->set_editable(false);
 
$viewPort= new GtkViewPort();
$textView= new GtkScrolledWindow();

$viewPort->add($textarea);
$textView->add($viewPort);

$Vbox= new GtkTable(30,30);
$Vbox->attach($textView,0,30,1,30,Gtk::FILL|Gtk::EXPAND);

$filebox= new GtkEntry();
$Vbox->attach($filebox,1,29,0,1,Gtk::FILL|Gtk::EXPAND);

$submit= new GtkButton('Reword Text');
$submit->connect_simple('clicked','func_generate');
$Vbox->attach($submit,0,1,0,1,Gtk::FILL|Gtk::EXPAND);
$filebtn= new GtkButton('Select File');
$filebtn->connect_simple('clicked','func_open');
$Vbox->attach($filebtn,29,30,0,1,Gtk::FILL|Gtk::EXPAND);
$wnd->add($Vbox);


 
$wnd->show_all();
Gtk::main();
?>

Dani AI

Generated

Short summary: tracked this down to the GUI text‑buffer call and fixed it by updating PHP‑GTK — the Windows "Not enough storage is available..." message in this thread turned out to be a symptom of an older PHP‑GTK build interacting badly with large single set operations. The rest of the replies are useful, but adding a couple of long‑term practices helps avoid the problem on older builds or when handling even bigger files.

For a robust workaround (and a general best practice), read and insert the file in chunks instead of building one huge string and replacing the whole buffer in a single call. Insert at the buffer end using the buffer iterator so the widget receives smaller, incremental updates; process each chunk (replace/clean) before inserting. Example pattern:

$fp = fopen($filename, 'r');
while (!feof($fp)) {
    $chunk = fgets($fp, 8192);   // read a chunk
    // process $chunk here (avoid breaking multi-byte sequences)
    $iter = $textBuffer->get_end_iter();
    $textBuffer->insert($iter, $chunk);
    // optionally let the main loop breathe between chunks
}
fclose($fp);

See the PHP‑GTK GtkTextBuffer docs and the GTK TextBuffer insert method for the exact APIs. (gtk.php.net)

Notes and cautions: chunked processing needs care with UTF‑8 (don't cut multi‑byte characters) and with regexes that can span chunk boundaries (use line-based reads or keep a small overlap between chunks). Long or blocking processing should be split into small tasks scheduled with the main loop (for example via timeouts/idle callbacks) so the UI stays responsive; also check PHP memory_limit and logs if allocation errors appear. The set/replace operation is documented to replace the entire buffer, so incremental insertion avoids the large single allocation that can trigger OS/extension allocation failures. (docs.gtk.org)

Bottom line: the upgrade that performed is the simplest fix, but the chunked/streaming pattern above is a durable fallback (and improves responsiveness and memory behavior) for any PHP‑GTK app that must display or process large text.

Found the problem. The following line is the bug.

$textareaBuffer->set_text($text);

The buffer can't hold more than 1024 characters. Does anybody know how to insert more?

Solved. Downloaded the latest version of php-gtk and the bug is fixed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.