willsherwood
asked on
PHP str_replace regex (simple wildcard)
I want to delete all occurrences of the following in a large string:
WITH having a wildcard for the date (which is well-formed (machine-generated))
thus (if * is wildcard)
<a href='#' class='calendarInquiryDate' onclick="updateArrivalDate('09/21/2011'); return false;" >
WITH having a wildcard for the date (which is well-formed (machine-generated))
thus (if * is wildcard)
<a href='#' class='calendarInquiryDate' onclick="updateArrivalDate('*'); return false;" >
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The preg_replace function uses Perl compatible regular expressions, which allows you to use the wildcard as required.
ASKER
great, thanks!!
ASKER
Follow-up help requested.
The PHP preg call worked perfectly up to a point, which i cannot figure out "what's different" past that point- maybe a fresh set of eyes :).
ORIGINAL STRING CONTENT
RESULT AFTER PROCESSING THRU preg_replace
Thus the (calendar dates) 19 and 20 <a href> tags were removed
but 21 and thereafter (many more weeks not shown here too)
(I have a sep simple str_replace call to get rid of </a> (that was easy) )
any suggestions? it's very close to completely working.
thanks!
The PHP preg call worked perfectly up to a point, which i cannot figure out "what's different" past that point- maybe a fresh set of eyes :).
ORIGINAL STRING CONTENT
<td class='mini-cal-day ' align='center' ><a href='#' class='calendarInquiryDate' onclick="updateArrivalDate('09/19/2011'); return false;" >19</a></td>
<td class='mini-cal-day ' align='center' ><a href='#' class='calendarInquiryDate' onclick="updateArrivalDate('09/20/2011'); return false;" >20</a></td>
<td class='mini-cal-day ' align='center' ><a href='#' class='calendarInquiryDate' onclick="updateArrivalDate('09/21/2011'); return false;" >21</a></td>
<td class='mini-cal-day ' align='center' ><a href='#' class='calendarInquiryDate' onclick="updateArrivalDate('09/22/2011'); return false;" >22</a></td>
<td class='mini-cal-day ' align='center' ><a href='#' class='calendarInquiryDate' onclick="updateArrivalDate('09/23/2011'); return false;" >23</a></td>
RESULT AFTER PROCESSING THRU preg_replace
<td class='mini-cal-day ' align='center' >19</td>
<td class='mini-cal-day ' align='center' >20</td>
<td class='mini-cal-day ' align='center' ><a href='#' class='calendarInquiryDate' onclick="updateArrivalDate('09/21/2011'); return false;" >21</td>
<td class='mini-cal-day ' align='center' ><a href='#' class='calendarInquiryDate' onclick="updateArrivalDate('09/22/2011'); return false;" >22</td>
<td class='mini-cal-day ' align='center' ><a href='#' class='calendarInquiryDate' onclick="updateArrivalDate('09/23/2011'); return false;" >23</td>
Thus the (calendar dates) 19 and 20 <a href> tags were removed
but 21 and thereafter (many more weeks not shown here too)
(I have a sep simple str_replace call to get rid of </a> (that was easy) )
any suggestions? it's very close to completely working.
thanks!
ASKER
p.s. the "original" html is machine-generated (not by me), and maybe it has some hidden characters in it? that is causing a mismatch.
but ALL the rest of the calendar weeks from that point on (9/21/2011) have the same result (tag not deleted)
but ALL the rest of the calendar weeks from that point on (9/21/2011) have the same result (tag not deleted)
Could we relax the pattern so it just looks for an a tag containing:
class='calendarInquiryDate '
class='calendarInquiryDate
eg this is a lot more flexible:
$string = preg_replace("@<a[^>]*clas s\s*=\s*'c alendarInq uiryDate'[ ^>]*>@s"," ",$string) ;
$string = preg_replace("@<a[^>]*clas
ASKER
yes any relaxed pattern should work - there are no other tags in the string
ASKER
perfect, all set.
again, thanks!
again, thanks!