PHP: Best way to extract text within parenthesis?

后端 未结 8 1481
北海茫月
北海茫月 2020-11-28 06:11

What\'s the best/most efficient way to extract text set between parenthesis? Say I wanted to get the string \"text\" from the string \"ignore everything except this (text)\"

8条回答
  •  旧时难觅i
    2020-11-28 06:37

    i'd just do a regex and get it over with. unless you are doing enough iterations that it becomes a huge performance issue, it's just easier to code (and understand when you look back on it)

    $text = 'ignore everything except this (text)';
    preg_match('#\((.*?)\)#', $text, $match);
    print $match[1];
    

提交回复
热议问题