简体   繁体   中英

Replacing link in a href tag by php

I have links like

<a href="#GGGGGGG31321">LINK TEXT</a>
<a href="#zzzz313GG31321">LINK TEXT 2</a>

...

ATTENTION! # symbol in begin is important, because i have and another links what i dont want to change

how i can replace by php it to:

<a href="#">LINK TEXT</a>
<a href="#">LINK TEXT 2</a>

Or better how i can leave link text, but remove a href tags fully

Thank you!

try this regex

 <?php
    $vv='<a href="#GGGGGGG31321">LINK TEXT</a>
    <a href="#zzzz313GG31321">LINK TEXT 2</a>';
    $vv=preg_replace('/<a(.*)href="([^"]*)"(.*)>/','<a$1href="#"$3>',$vv);
    echo $vv;
 ?>

test & work

I think this will work:

preg_replace( '/<a\shref=\".*\">(.+<\/a>)/', '<a>$1', $linkTag );

It should remove the whole href tag.

<?php
    $string = '<a href="#GGGGGGG31321">LINK TEXT</a><a href="#zzzz313GG31321">LINK TEXT 2</a>';
    $regex = '/<a\\shref=".*?">(.*?)<\\/a>/is';
    echo preg_replace($regex, '<a>$1</a>', $string);
?>

This returns: <a>LINK TEXT</a>

Maybe I didn't understand you when you said

Or better how i can leave link text, but remove a href tags fully

but hopefully it's helpful.

怎么样:

preg_replace('/\bhref="#[^"]+"/', 'href="#"', $string)

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