简体   繁体   中英

php regular expression match prbl about starting with one string but not the other

I have to replace some invalid links in html as following:

<td><a title="Michel Blanc" href="http://www.mysite.com/index.php?title=Michel_Blanc&amp;action=edit&amp;redlink=1">Michel Blanc</a></td>
<td><a title="Pierre Schöller" href="http://www.mysite.com/index.php?title=Pierre_Sch%C3%B6ller&amp;action=edit&amp;redlink=1">Pierre Schöller</a></td>
<td><a title="Focus Features" href="http://www.mysite.com/w/Focus_Features">Focus Features</a><br />
<a title="Olivier Treiner" href="http://www.mysite.com/index.php?title=Olivier_Treadfadfadfiner&amp;action=edit&amp;redlink=1">Olivier Treiner</a>
<td>1600</td>

I want to remove all <a> tags but keep the text between <a></a> if the href begins with

    http://www.mysite.com/index.php?title=

and keep the <a> tags if the href begins with

    http://www.mysite.com/w/

here is my regular expression

    (<a title="([\s\S])*?" href="http://www\.mysite\.com/index\.php\?title=([\s\S])*?&amp;action=edit&amp;redlink=1">([\s\S])*?</a>)

but it involves the third line that I want to keep. I tested it in http://regexpal.com/

anybody help me?

这个为我工作:

(<a title="[^>]*?" href="http://www\.mysite\.com/index\.php\?title=([\s\S])*?&amp;action=edit&amp;redlink=1">([\s\S])*?</a>)
$subject = <<<'LOD'
<td><a title="Michel Blanc" href="http://www.mysite.com/index.php?title=Michel_Blanc&amp;action=edit&amp;redlink=1">Michel Blanc</a></td>
<td><a title="Pierre Schöller" href="http://www.mysite.com/index.php?title=Pierre_Sch%C3%B6ller&amp;action=edit&amp;redlink=1">Pierre Schöller</a></td>
<td><a title="Focus Features" href="http://www.mysite.com/w/Focus_Features">Focus Features</a><br />
<a title="Olivier Treiner" href="http://www.mysite.com/index.php?title=Olivier_Treadfadfadfiner&amp;action=edit&amp;redlink=1">Olivier Treiner</a>
<td>1600</td>
<a href="http://remove.me.com">remove.me</a>
LOD;

A regex way:

$pattern = <<<'LOD'
~
# definitions
(?(DEFINE)
  # all the content from the "a" tag begining until the content 
  # of the "href" attribute 
  (?<atohref>
      <a\b (?> [^h>]++ | \Bh | h(?!ref) )++ href\s*+=\s*+['"]?+
  )

  # all the content until the closing "a" tag 
  (?<untilclosea>
      (?> [^<]++ | <(?!/a>) )++
  )
)

# pattern
    \g<atohref>
    \Qhttp://www.mysite.com/\E
    (?>
        \Qindex.php?title=\E
        [^>]*+>
        ( \g<untilclosea> ) # third group (because of the two named groups)
        </a>
      |
        w/ \g<untilclosea>
        </a> \K      # reset the match (to preserve it)
    )
  | 
    <a\b \g<untilclosea> </a> # all other "a" tags
~x
LOD;

$replacement = '$3';
$result = preg_replace($pattern, $replacement, $subject);
echo htmlspecialchars($subject).'<br><br>';
echo htmlspecialchars($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM