Gmunky Posted March 2, 2007 Share Posted March 2, 2007 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! Link to comment https://forums.phpfreaks.com/topic/40803-need-help-with-extracting-part-of-a-text-in-a-text-file/ Share on other sites More sharing options...
itsmeArry Posted March 2, 2007 Share Posted March 2, 2007 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); Link to comment https://forums.phpfreaks.com/topic/40803-need-help-with-extracting-part-of-a-text-in-a-text-file/#findComment-197616 Share on other sites More sharing options...
Gmunky Posted March 3, 2007 Author Share Posted March 3, 2007 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> Link to comment https://forums.phpfreaks.com/topic/40803-need-help-with-extracting-part-of-a-text-in-a-text-file/#findComment-198514 Share on other sites More sharing options...
Orio Posted March 3, 2007 Share Posted March 3, 2007 <?php $text = file_get_contents("file.txt"); preg_match_all("(<ECHOTYPE3>.*?<\/ECHOTYPE3>)", $text, $matches); $the_part_you_want = $matches[0][0]; ?> Orio. Link to comment https://forums.phpfreaks.com/topic/40803-need-help-with-extracting-part-of-a-text-in-a-text-file/#findComment-198527 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.