Search code examples
phpregexpreg-match

exclude results found inside anchor text


I am trying to use regex in php to find all the words count excluding the ones located in the anchor text

I start with /\b(count)\b/i but can not get exactly what I need

Cycle count Stored as a <a href="poem://plaspoem/POEM?LOC=MAIN&CMD=VIEW&KEY=CYCLE COUNT&REV=A">Cycle count</a> which is a count of records Cycle count`

Solution

  • You'll can use negative lookahead:

    /count(?!<\/a>)(?!&)/i
    

    Matches: Cycle count Stored as a <a href="poem://plaspoem/POEM?LOC=MAIN&CMD=VIEW&KEY=CYCLE COUNT&REV=A">Cycle count</a> which is a count of records Cycle count

    EXAMPLE

    For your second request you would just use a negative look ahead to ignore the word followed by a space and 'time':

    count(?!&)(?!\stime)
    

    EXAMPLE

    Matches: Cycle count Stored as a <a href="poem://plaspoem/POEM?LOC=MAIN&CMD=VIEW&KEY=CYCLE COUNT&REV=A">Cycle count time</a> which is a count of records Cycle count

    Now, a word of warning: this is pretty fragile if you do not know what word will follow 'count' when used between the brackets.