Regular Expression to Search+Replace href=“URL”

前端 未结 2 1486
感情败类
感情败类 2021-01-01 02:40

I\'m useless with regular expressions and haven\'t been able to google myself a clear solution to this one.

I want to search+replace some text ($content) for any url

2条回答
  •  星月不相逢
    2021-01-01 03:04

    Here is the gist of what I came up with. Hopefully it helps someone:

    $content = get_the_content();
    $pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
    $newurl = get_permalink();
    $content = preg_replace($pattern,$newurl,$content);
    
    echo $content;
    

    Mucho thanko to @WiseGuyEh

提交回复
热议问题