solarisuser Posted July 17, 2008 Share Posted July 17, 2008 Hi All, I have a text file that contains directory paths. My goal is to delete the line from the text file if it has exactly two "\" characters, no more and no less. I've thought about using count_chars() but I can't figure out the best way to integrate it with preg_match_replace or something of the like. Any help would be much appriciated! Thanks Link to comment https://forums.phpfreaks.com/topic/115283-solved-delete-or-replace-with-quotquot-a-line-in-a-text-file-if-has-exactly-two-quot-c/ Share on other sites More sharing options...
dannyb785 Posted July 17, 2008 Share Posted July 17, 2008 I don't have(or know) the exact code, but it should be something like this: -read file A line by line -write each line to file B, as long as it's not blank(or long as it's not whatever line you want to not be printed), so you'd do a conditional[ if($data != "whatever") fprintf(something); ] or something to that effect. Look up fopen, fprintf, and similar functions, then you'll gert an idea of how to write the code for it. that's pretty much it. Link to comment https://forums.phpfreaks.com/topic/115283-solved-delete-or-replace-with-quotquot-a-line-in-a-text-file-if-has-exactly-two-quot-c/#findComment-592690 Share on other sites More sharing options...
solarisuser Posted July 17, 2008 Author Share Posted July 17, 2008 thanks but i got that part down no problem. the thing is, while im reading each line of text i need to use some regex to find out if that line has two occurances of "\" and delete that line, then continue... I need to use regex because the data is dynamically generated Link to comment https://forums.phpfreaks.com/topic/115283-solved-delete-or-replace-with-quotquot-a-line-in-a-text-file-if-has-exactly-two-quot-c/#findComment-592694 Share on other sites More sharing options...
corbin Posted July 18, 2008 Share Posted July 18, 2008 /^[\\]{1}[^\\]*?[\\]{1}[^\\]$*/ or just if(substr_count($str, "\\") == 2) Link to comment https://forums.phpfreaks.com/topic/115283-solved-delete-or-replace-with-quotquot-a-line-in-a-text-file-if-has-exactly-two-quot-c/#findComment-593092 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.