2

I have the following regex:

(<div class="dotted-highlight">(.*?)\s*){2,}<ul>

which is matching the following string:

<div class="dotted-highlight">The cover letter should include: <div class="dotted-highlight"><ul>

And I need to access The cover letter should include so I tried:

    <?php
    $textarea = '<div class="dotted-highlight">The cover letter should include: 
<div class="dotted-highlight"><ul>';
    $replacing_to = '(<div class="dotted-highlight">(.*?)\s*){2,}<ul>';
    $replacing_with = '$1<ul>';
    $textarea = preg_replace('#'.$replacing_to.'#', $replacing_with, $textarea);
    ?>

$replacing_with adds <div class="dotted-highlight"> instead of the desired text: The cover letter should include:

So the output would be:

<ul>

and Expected OUTPUT would be:

The cover letter should include: <ul>

FIDDLE:

http://codepad.org/j8EnRBFI

REGEX TESTER

http://www.regexr.com/3bkb8

12
  • strip_tags($textarea, '<ul>') Commented Aug 19, 2015 at 17:45
  • @JonathanKuhn Can't do that string is much larger i only added the point that I want to make, how to match subpattern. in this case. Commented Aug 19, 2015 at 17:46
  • Your replacement is going to probably need to use $2 as it is the second group that is the (.*?). Commented Aug 19, 2015 at 17:49
  • doesn't work with $2 either it's a subpatter also tried with $1\1 and it outputted <div class="dotted-highlight"> Commented Aug 19, 2015 at 17:50
  • $1 is the div, which is the first group. $2 should be the .* which is what you want. Commented Aug 19, 2015 at 17:52

1 Answer 1

1

Just use this and use the second capture group:

((<div class="dotted-highlight">)([^<\n]*)\s?).*?\2.*?<ul>

Regex101

3v4l

Sign up to request clarification or add additional context in comments.

2 Comments

It works, but not as the old one, I added {2,} because the replacement should take place if there are 2 or more patterns, right now if I remove the first <div class="dotted-highlight"> it will match the second one and it should not.
@Adrian try my updated regular expression. All of the examples have been updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.