Remove XML Node from a file loaded using PHP filesystem [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 4 years ago.
I have an XML file that's read using PHP's file_get_contents so other changes can be done to it.
I need to find and remove the nodes <BATCHALLOCATIONS.LIST>...<BATCHALLOCATIONS.LIST> (not just those two lines, but what's between the entire node) in the entire file.
Since the file is already loaded using file_get_contents I'd like to do this without having to load the file again using simpleXML, or an XML parser or any other method (like DOM).
The node does not have a specific parent and appears randomly.
The XML file is exported from a Business Accounting Software.
Any idea on how to achieve this? Maybe using a Regular Expression to do a search and replace or something like that?
I've been trying to do this using a regular expression and preg_replace, but just can't get things to work.
Here's just a portion of the file. The original runs to 10K+ lines.
This should have worked but doesn't
preg_replace('/^\<BATCHALLOCATIONS.LIST\>(.*?)\<\BATCHALLOCATIONS.LIST\>$/ism','', $newXML);
I'm trying to do this without using any HTML/XML parser.

There's probably a better way to do it, but this will work
// get your file as a string
$yourXML = file_get_contents($file) ;
$posStart = stripos($yourXML,'<BATCHALLOCATIONS.LIST>') ;
$posEnd = stripos($yourXML,'</BATCHALLOCATIONS.LIST>') + strlen('</BATCHALLOCATIONS.LIST>') ;
$newXML = substr($yourXML,0,$posStart) . substr($yourXML,$posEnd) ;

Related

Read only Title from a page? [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I have a page called index.php which has some content on it, but the title is set from another server that generates a text file called cache_currentsongcallapi.txt
Contents of 'cache_currentsongapi.txt':
<tracks>
<radioname>Mick&apos;s Music Station</radioname>
<rank>0</rank>
<isradionomy>1</isradionomy>
<radurl>http://www.radionomy.com/mick-smusicstation</radurl>
<track>
<uniqueid>2722440231</uniqueid>
<title>Hello</title>
<artists>Adele</artists>
<starttime>2015-11-23 21:05:02.35</starttime>
<playduration>293023</playduration>
<current>1</current>
<callmeback>217256</callmeback>
</track>
</tracks>
Is there any way that I can take ONLY the title from the text file and display it in the index page?
NOTE:
This method cannot include editing the text file as it is overwritten by the server.
The website that this code will go on: http://mickyd.net/radio
This appears like valid XML. Never, never read XML using regular expressions. You'll hear this a lot, and for good reasons.
I'd start by reading the string as XML:
$xml = new SimpleXMLElement($string);
$result = $xml->xpath('/tracks/track/title');
Your mileage may vary, but basically you can do a lot starting from here. I'd suggest reading a bit more on DOMXPath and SimpleXMLElement.
Here's a full version including the reading of the actual file since you seem to need help there too, but please mark Victor's as the answer.
$filePath = "file.txt";
$handle = fopen($filePath, "r");
$fileContents = fread($handle, filesize($filePath));
$xml = new SimpleXMLElement($fileContents);
$title = $xml->xpath("/tracks/track/title");
echo($title[0]);
Where "file.txt" is the path to the file.

Read ini file in PHP, not parse [duplicate]

This question already has answers here:
PHP: Escape illegal chars in .ini-files
(2 answers)
Closed 1 year ago.
When using parse_ini_file, it will attempt to read and understand the ini file. I'm looking for a solution that will read the file, place it into arrays much like parse_ini_file does, however I don't want it to get to the special characters in my ini file and spit an error that it can't parse them. Could anyone point me in the right direction?
ini file for reference:
[IMPORT]
email=email#thisisanemail.com
location=
Description=Order Form
name=*.xls*
matrixfile=
matrix_field=
matrix_disc_type_column=
matrix_disc_percentage_column=
fixed=XLS
separator=|
RowTerminator=
headerrows=13
footerrows=0
maxexcelcolumns=7
If I parse this, it gets stuck on the seperator "|" but I need that there.
If you use parse_ini_file with the scanner_mode parameter to specify INI_SCANNER_RAW, then option values will not be parsed (see
https://www.php.net/manual/en/function.parse-ini-file.php).

Load and parse XML file with PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have to create a webservice which goes to a specific URL that returns a XML-file as response and interprets/parses this file in order to save its contents to a MySQL database.
I've heard about the SimpleXML but I'm not sure how to get the websites response into a file whose path is needed in order to parse the document.
Can somebody at least explain me how to reach the goal of downloading the XML and saving it to a file? (best with some PHP code)
I will then (hopefully) find out by myself how to parse it and store its contents.
Here's an example of what my XML will look like (for privacy reasons I can't publish the real URL I'm using...)
Here's a couple of pointers..
To download a file and save it, the easiest way I have found is this:
<?php
file_put_contents('saved.xml', file_get_contents('http://www.xmlfiles.com/examples/simple.xml'));
You can then open the file with the simpleXML library like so:
$xml = simplexml_load_file('saved.xml');
var_dump($xml);
Hope that gives you enough info to get started.
See simpleXML for info on the simpleXML library.
You can download and save the xml to a local file by doing this:
$xmlstring = file_get_contents("http://domain.com/webservice/xmlfile.xml");
file_put_contents("path/localxmlfile.xml", $xmlstring);
To parse the xml file I suggest you to use DOMDocument class in combination with the DOMXPath class to query/search for specific elements.
DOMDocument: http://php.net/manual/de/class.domdocument.php
DOMXPath: http://php.net/manual/de/class.domxpath.php
Hopefully you can find your answer on below link. Seems related topic.
How do you parse and process HTML/XML in PHP?

JSON wrapped in some HTML, remove HTML [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to parse and process HTML with PHP?
I'm trying to scrape a page with PHP using file_get_contents().
This page has some JSON wrapped in a bit of HTML. I'd like to strip out this HTML to be able to use json_decode() on the scraped string so I can deal with the JSON separately.
Is there any clean way to do that? A quick search didn't really lead to anything.
Thanks
parsing/stripping HTML content is always a tricky one because (common?) solutions via regex might crash if the HTML markup is malformed and are painful slow btw. I would suggest using this little HTML DOM parser class:
http://simplehtmldom.sourceforge.net/
edited & added from subcomment:
Okay this is a bad one because the inline javascript is not properly wrapped with CDATA-Tags. Otherwise something like this might work:
$html = new simple_html_dom();
$html->load_file('your-external-file');
foreach($html->find("script") as $obj) {
if(isset($obj->innertext) && strpos($obj->innertext, 'window._jscalls'))
echo $obj->innertext;
}

How to detect new line from the parsed XML with simpleload_xml_string? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP SimpleXML doesn't preserve line breaks in XML attributes
I have following XML
$xmldatas = '<layer text="name
id"></layer>';
I have parse this XML with
$xml = simplexml_load_string($xmldatas);
But when I checked the $xml, the \n is been replaced with space. I want the new line remains as it is after the xml parsing.
But how can I do that ?
Thanks
I don't think that xml will accept a new line character in the tag option text.
If you are generating the xml maybe you want to do something like this?
$xmldatas = '<layer><text>name
id</text></layer>';

Categories