开发者

PHP extract string in between [ and ]

开发者 https://www.devze.com 2023-01-12 10:51 出处:网络
How to extract string between [ and ] I tried using trim() but I believe trim() only works in remov开发者_开发技巧ing spaces.

How to extract string between [ and ]

I tried using trim() but I believe trim() only works in remov开发者_开发技巧ing spaces.

ex. [abc123-ab:ab123] output should be -> abc123-ab:ab123


Trim will work if you give it that second argument:

trim($str, '[]');

but if the the "[]"s will always be on the beginning and end of the string (they need to be for trim to work), a better solution might be substr:

substr($str, 1, -1);


function between($t1,$t2,$page) {
    $p1=stripos($page,$t1);
    if($p1!==false) {
        $p2=stripos($page,$t2,$p1+strlen($t1));
                if($p2===false) { return false; }
    } else {
        return false;
    }
    return substr($page,$p1+strlen($t1),$p2-$p1-strlen($t1));
}

$mytext=between('[',']',$str);
if($mytext!==false) {
   //do something here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消