如何在PHP中使用起始字符和结束字符获取字符串的子字符串?
我希望在PHP中使用起始标记和结束标记查找字符串的中间部分如何在PHP中使用起始字符和结束字符获取字符串的子字符串?,php,Php,我希望在PHP中使用起始标记和结束标记查找字符串的中间部分 $str = 'Abc/hello@gmail.com/1267890(A-29)'; $agcodedup = substr($str, '(', -1); $agcode = substr($agcodedup, 1); agcode的最终预期值: $agcode = 'A-29'; 您可以使用preg_match $str = 'Abc/hello@gmail.com/1267890(A-29)'; if(
$str = 'Abc/hello@gmail.com/1267890(A-29)';
$agcodedup = substr($str, '(', -1);
$agcode = substr($agcodedup, 1);
agcode的最终预期值
:
$agcode = 'A-29';
您可以使用preg_match
$str = 'Abc/hello@gmail.com/1267890(A-29)';
if( preg_match('/\(([^)]+)\)/', $string, $match ) ) echo $match[1]."\n\n";
输出
A-29
你可以在这里查看
基本上正则表达式表示:
- 开始匹配,匹配
打开Paren-literal\(
- 捕获组
(..)
- 匹配除
关闭参数以外的所有内容[^]+
)
- 结束匹配,匹配
关闭Paren-literal\)
substr
,那就来吧:
$str = 'Abc/hello@gmail.com/1267890(A-29)';
//this is the location/index of the ( OPEN_PAREN
//strlen 0 based so we add +1 to offset it
$start = strpos( $str,'(') +1;
//this is the location/index of the ) CLOSE_PAREN.
$end = strpos( $str,')');
//we need the length of the substring for the third argument, not its index
$len = ($end-$start);
echo substr($str, $start, $len );
输出
A-29
你可以在这里测试
如果是我,我会对这两种方法进行基准测试,看看哪种更快。您可以使用preg\u match
$str = 'Abc/hello@gmail.com/1267890(A-29)';
if( preg_match('/\(([^)]+)\)/', $string, $match ) ) echo $match[1]."\n\n";
输出
A-29
你可以在这里查看
基本上正则表达式表示:
- 开始匹配,匹配
打开Paren-literal\(
- 捕获组
(..)
- 匹配除
关闭参数以外的所有内容[^]+
)
- 结束匹配,匹配
关闭Paren-literal\)
substr
,那就来吧:
$str = 'Abc/hello@gmail.com/1267890(A-29)';
//this is the location/index of the ( OPEN_PAREN
//strlen 0 based so we add +1 to offset it
$start = strpos( $str,'(') +1;
//this is the location/index of the ) CLOSE_PAREN.
$end = strpos( $str,')');
//we need the length of the substring for the third argument, not its index
$len = ($end-$start);
echo substr($str, $start, $len );
输出
A-29
你可以在这里测试
如果是我,我会对这两种方法进行基准测试,看看哪种更快。希望这对您有所帮助
function getStringBetween($str, $from, $to, $withFromAndTo = false)
{
$sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
if ($withFromAndTo) {
return $from . substr($sub,0, strrpos($sub,$to)) . $to;
} else {
return substr($sub,0, strrpos($sub,$to));
}
$inputString = "Abc/hello@gmail.com/1267890(A-29)";
$outputString = getStringBetween($inputString, '(', ')');
echo $outputString;
//output will be A-29
$outputString = getStringBetween($inputString, '(', ')', true);
echo $outputString;
//output will be (A-29)
return $outputString;
}
愿这对你有帮助
function getStringBetween($str, $from, $to, $withFromAndTo = false)
{
$sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
if ($withFromAndTo) {
return $from . substr($sub,0, strrpos($sub,$to)) . $to;
} else {
return substr($sub,0, strrpos($sub,$to));
}
$inputString = "Abc/hello@gmail.com/1267890(A-29)";
$outputString = getStringBetween($inputString, '(', ')');
echo $outputString;
//output will be A-29
$outputString = getStringBetween($inputString, '(', ')', true);
echo $outputString;
//output will be (A-29)
return $outputString;
}
需要获取介于“(”和“)”之间的字符串。谢谢。需要在“(“和”)之间设置字符串。非常感谢。