sasquatch69 Posted November 27, 2007 Share Posted November 27, 2007 I searched for this on the forums but did not find an answer to my question, so here goes: I am trying to find a way to strip out everything between two characters in a string. The php script will be pulling text from a separate html file that automatically updates itself with new content on an hourly basis (designed to pull into string $rawtext via file_get_contents). One of the information fields in the file (which are separated by HTML tables, which I am using str_replace to change into divs for standards compliance) contains the start and end time of an event, and pulls into a div class of its own on my page in a hh:mm am/pm to hh:mm am/pm format (e.g. "10:00am - 11:00am" or "12:00pm - 1:00pm"). I only want to display the beginning time without the hyphen and the ending time, so that it just reads "10:00am", but I have no access to change how the file is output (here is the code chunk: "<TD>12:00PM - 2:00PM</TD>"). From what I can tell, I can't use str_replace because that end time will be constantly changing. And I can't truncate how much text is pulled in from the file because there are other fields after the time in the file that I will need. So, is there a better way to strip the "- hh:mmtt" from the middle of the file? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted November 27, 2007 Share Posted November 27, 2007 This might work <?php $time = '12:00pm - 1:00pm'; list($sTime,$hyph,$eTime) = explode(' ', $time); echo $sTime.'<br>'; echo $hyph.'<br>'; echo $eTime.'<br>'; ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 27, 2007 Share Posted November 27, 2007 Are these the only two times in the data? If so, something like this might work for you: <?php $str = "other text <TD>12:00PM - 2:00PM</TD> other text"; $str = preg_replace('% - [0-9]{1,2}:[0-9]{2}(A|P)M%','',$str); echo $str; ?> Quote Link to comment Share on other sites More sharing options...
sasquatch69 Posted November 27, 2007 Author Share Posted November 27, 2007 Sorry, I didn't do a great job of explaining. The times will change every hour to reflect the current hour (and the hour immediately following), which is why I can't just put a certain time down and then use a function to replace it. I just used the 12:00pm - 2:00pm as an example. Thus, I am trying to find a way to strip out everything from the hyphen until the </TD>, regardless of what time it says. Thanks! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 27, 2007 Share Posted November 27, 2007 Did you try my suggestion? It uses regular expressions and would work with any times. Quote Link to comment Share on other sites More sharing options...
sasquatch69 Posted November 27, 2007 Author Share Posted November 27, 2007 GingerRobot, thanks for the tip and clarification. I tried your suggestion and it worked perfectly! I appreciate everyone's help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.