Regex on this string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Regex on this string

Post by klevis miho »

What regex do I need on this string:

<span class="test">06:00</span> <span class="information"><a href="http://example.com/tesTing/0,123.html">Test</a>

to get this result:

06:00</span> <span class="information"><a>Test

The string's have have the same structure except for a variable url in the href.

I have this regex: #<span class="test">(.+?)</a>#s but it displays me the string with the actual href.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Regex on this string

Post by tr0gd0rr »

Kind of a strange text to search and find. I hope your reasoning is mostly academic.

Code: Select all

$test = '<span class="test">06:00</span> <span class="information"><a href="http://example.com/tesTing/0,123.html">Test</a>';
$regex = '#<span class="test">(.+?<a)[^>]+(>.+?)</a>#s';
$new = preg_replace($regex, $test, '$1$2'); // '06:00</span> <span class="information"><a>Test'
Post Reply