Jump to content

[SOLVED] removing text between two characters in a string


sasquatch69

Recommended Posts

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?

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;
?>

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!

 

 

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.