How to remove text between tags in php?

前端 未结 6 932
时光取名叫无心
时光取名叫无心 2020-12-06 00:56

Despite using PHP for years, I\'ve never really learnt how to use expressions to truncate strings properly... which is now biting me in the backside!

Can anyone prov

6条回答
  •  心在旅途
    2020-12-06 01:21

    You don't need to capture the tags themselves. Just target the text between the tags and replace it with an empty string. Super simple.

    Demo of both techniques

    Code:

    $string = 'text';
    echo preg_replace('/]*>\K[^<]*/', '', $string);
    // the opening tag--^^^^^^^^  ^^^^^-match everything before the end tag
    //                          ^^-restart fullstring match
    

    Output:

    
    

    Or in fringe cases when the link text contains a <, use this: ~]*>\K.*?(?=)~

    This avoids the expense of capture groups using a lazy quantifier, the fullstring restarting \K and a "lookahead".


    Older & wiser:

    If you are parsing valid html, you should use a dom parser for stability/accuracy. Regex is DOM-ignorant, so if there is a tag attribute value containing a >, my snippet will fail.

    As a narrowly suited domdocument solution to offer some context:

    $dom = new DOMDocument;
    $dom->loadHTML($string, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); // 2nd params to remove DOCTYPE);
    $dom->getElementsByTagName('a')[0]->nodeValue = '';
    echo $dom->saveHTML();
    

提交回复
热议问题