Jump to content

Need help with extracting part of a text in a text file


Gmunky

Recommended Posts

I am having trouble trying to save part of the text file to a string variable.

 

my text file looks something like this:

 

<HTML><HEAD><title>ECHOnline Response</title></HEAD><BODY>

<!-- <ECHOTYPE1><status>G</status><id>erere</id><ECHOTYPE1> -->

<ECHOTYPE2><table><tr><td>g</td><td>erere</td></tr></table><ECHOTYPE2>

<!-- <ECHOTYPE3><status>G</status><id>erere</id><auth>3</auth></ECHOTYPE3> -->

</BODY></HTML>

 

 

what I want to do is to be able to just save this part of the file in a string variable:

<ECHOTYPE3><status>G</status><id>erere</id><auth>3</auth></ECHOTYPE3>

 

thank you for all your help!

1. open the file.

2. read the content of file into a variable

3. get the position of <ECHOTYPE3> using $pos1 = strpos($mystring, "<ECHOTYPE3>");

4. get the position of </ECHOTYPE3> using $pos2 = strpos($mystring, "</ECHOTYPE3>");

5. now use substring function to get the string between these two positions.

substr($mystring, $pos1, $pos2); 

 

when I try your method:

 

$file=fopen("file.txt","r")or exit("Unable to open file!");

while (!feof($file))

{

$string .= fgets($file);

}

fclose($file);

 

$pos1 = strpos($string, "<ECHOTYPE3>");

$pos2 = strpos($string, "</ECHOTYPE3>");

 

$xmlstring=substr($string, $pos1, $pos2);

echo $xmlstring;

 

with this code, I still get extra characters that I don't want:

here's my output:

 

<ECHOTYPE3><status>G</status><id>erere</id><auth>3</auth></ECHOTYPE3> -->

</BODY></HTML>

 

i dont want this to show up in the output: --></BODY></HTML>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.