我需要拆分以下字符串.
$string = "This is string sample - $2565";
$split_point = " - ";
Run Code Online (Sandbox Code Playgroud)
一:我需要能够使用正则表达式或任何其他匹配将字符串拆分为两部分,并指定要拆分的位置.
第二:也想为$做一个preg_match然后只在$右边抓一个数字.
有什么建议?
$split_string = explode($split_point, $string);
Run Code Online (Sandbox Code Playgroud)
和
preg_match('/\$(\d*)/', $split_string[1], $matches);
$amount = $matches[1];
Run Code Online (Sandbox Code Playgroud)
如果你愿意,这可以在一个正则表达式中完成:
$pattern = '/^(.*)'.preg_quote($split_point).'\$(\d*)$/'
preg_match($pattern, $string, $matches);
$description = $matches[1];
$amount = $matches[2];
Run Code Online (Sandbox Code Playgroud)