dtng25909 2013-09-08 23:33
浏览 115
已采纳

获取两个字符串php之间的子字符串

suppose i have a set of strings formatted in a way:

$string1 = "(substring1) Hello";
$string2 = "(substring2) Hi";

how do i get the inside of '()' as a single string. I know this is possible by using:

$substring = explode( ")",$string1 );

but then i'm left with $substring[0] containing "(substring", and $substring[1] containing the rest. Is there a fast wat to get the inside of '()'? thanks i know it's a no brainer question.

  • 写回答

3条回答 默认 最新

  • duanlumei5941 2013-09-08 23:35
    关注

    explode might need a workaround because ( and ) are different. A simple regex will get the job done.

    $string1 = "(substring1) Hello";
    preg_match('#\((.*?)\)#', $string1, $match);  // preg_match_all if there can be multiple such substrings
    print $match[1];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部