preg_replace to exclude PHP

前端 未结 2 1382
清歌不尽
清歌不尽 2021-01-20 02:05

Im using preg_replace to replace keywords in text with a href tag, my regex is working awesome, right now my code is:

$newstring2 = preg_replace(\"/\\p{L}*?\         


        
2条回答
  •  失恋的感觉
    2021-01-20 02:49

    How about this with negative lookahead. Regex

    Explanation: capture all the keyword that is called text and replace with it some link but don't capture those keywords that have after it.

    $re = '/(text)(?!<\/a>)/m';
    $str = 'this is sample text about something what is text.
    
    this is sample text about something what is text.';
    $subst = '$1';
    
    $result = preg_replace($re, $subst, $str);
    
    echo $result;
    

    Output:

    this is sample text about something what is text. 
    
    this is sample text about something what is text.
    

    DEMO: https://3v4l.org/DVTB1

提交回复
热议问题