Solved

PHP str_replace regex (simple wildcard)

Posted on 2011-09-20
9
494 Views
Last Modified: 2012-05-12
I want to delete all occurrences of the following in a large string:

<a href='#' class='calendarInquiryDate'  onclick="updateArrivalDate('09/21/2011');  return false;" >

Open in new window


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

Open in new window

 
0
Comment
Question by:willsherwood
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 36570275
$string = preg_replace("@<a href='#' class='calendarInquiryDate'  onclick=\"updateArrivalDate('[^']*');  return false;\" >@","",$string);
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36570295
The preg_replace function uses Perl compatible regular expressions, which allows you to use the wildcard as required.
0
 

Author Closing Comment

by:willsherwood
ID: 36570340
great, thanks!!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:willsherwood
ID: 36570557
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
<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>

Open in new window


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>

Open in new window


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!
0
 

Author Comment

by:willsherwood
ID: 36570562
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)
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36570571
Could we relax the pattern so it just looks for an a tag containing:
class='calendarInquiryDate'
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 36570597
eg this is a lot more flexible:

$string = preg_replace("@<a[^>]*class\s*=\s*'calendarInquiryDate'[^>]*>@s","",$string);
0
 

Author Comment

by:willsherwood
ID: 36570603
yes any relaxed pattern should work - there are no other tags in the string
0
 

Author Comment

by:willsherwood
ID: 36570646
perfect, all set.
again, thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question