Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a string using preg_match

Tags:

regex

php

I'm having trouble using preg_match to find and replace a string. The string of interest is:

<span style="font-size:0.6em">EXPIRATION DATE: 04/30/2011</span>

I need to target and replace the date, "04/30/2011" with a different date. Can someone throw me a bone a give me the regular expression to match this pattern using preg_match in PHP? I also need it to match in such a way that it only replaces up to the first closing span and not closing span tags later in the code, e.g.:

<span style="font-size:0.6em">EXPIRATION DATE: 04/30/2011</span><span class="hello"></span>

I'm not versed in regex, and although I've spent the last hour trying to learn enough to make this work, I'm utterly failing. Thanks so much!

EDIT: As you can see this has gotten me exhausted. I did mean preg_replace, not preg_match.

like image 476
tollmanz Avatar asked Jan 24 '26 08:01

tollmanz


1 Answers

If you're after a replacement, consider using preg_replace(), something like

preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '<new date>', $string);
like image 146
Phil Avatar answered Jan 26 '26 21:01

Phil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!