0

I'm new to PHP (and programming generally) and I'm working on a project that should download a large (around 85mb) XML file from a website, search it for a string, and then edit the text at the string position. It seems like there are a lot of ways to do the searching part using PHP and other languages. Could someone recommend the most efficient or some further reading? - Thanks.

EDIT: I should have mentioned that many of the strings I'll be searching for will be non-XML/plain text. So I can't rely on any XML formatting. Will those XML tools still be useful for that? Or should I use something else? Would something as simple as strpos() work? (but could I use an 85mb file as the haystack?)

OutThere
  • 467
  • 2
  • 8
  • 19
  • If that is an XML file, you normally first want to parse the XML and then edit the plain text string (often an XML node value). For your question about the 85mb string and strpos please see: [What is the maximum length of a String in PHP?](http://stackoverflow.com/q/3189040/367456). – hakre Aug 27 '15 at 17:08

2 Answers2

0
$file = "your.xml";
$doc = new DOMDocument();
$doc->load($file);

Its support XPath.

If XML more 100-150 mb, recomended send xml to local application, like on C or Java, proccecing, put in directory, and get directory/filename in PHP.

Its more fast, than processing in big data in PHP.

SarDau Mort
  • 187
  • 4
0

You'll want to use the XmlReader and XmlWriter. They are SAX like xml processors that don't require loading the complete document into memory.

The API is fairly lowlevel. The hakre/xmlreaderiterator package provides a higher level API for streaming xml transformations. The following code converts all text nodes to uppercase and prints the result to stdout:

<?php

require 'vendor/autoload.php';

$reader = new XMLReader();
$reader->open('https://raw.githubusercontent.com/hakre/XMLReaderIterator/master/examples/data/movies.xml');

$writer = new XMLWriter();
$writer->openUri('php://output');
$writer->startDocument();

$iterator = new XMLWritingIteration($writer, $reader);

foreach ($iterator as $node) {
    if ($node->nodeType === XMLReader::TEXT) {
        // operate on text nodes
        $writer->writeRaw(strtoupper($node->value));
    } else {
        // copy everything else as is
        $iterator->write();
    }
}
Peter
  • 29,454
  • 5
  • 48
  • 60