如何在下面修改此功能以获取数字之间的字符串?
功能:
function getInbetweenStrings($start, $end, $str){
$matches = array();
$regex = "/$start([a-zA-Z0-9_]*)$end/";
preg_match_all($regex, $str, $matches);
return $matches[1];
}
示例:
1448459casa48912687
010697046a-parede-verde698012
456902sim-senhora4401254
预期输出:
casa
a-parede-verde
sim-senhora
谢谢。 @edit:我不想删除所有数字,我需要它们!我只想在它们之间抓住绳子。
答案 0 :(得分:2)
您可以使用此正则表达式来查找起始数字和尾随数字之间的最小字符序列(通过在?
上添加.*
)
^\d+(.*?)\d+$
在PHP中:
$strings = array('1448459casa48912687',
'010697046a-parede-verde698012',
'456902sim-senhora4401254',
'12345number10-downing-street54321');
foreach ($strings as $string) {
$between = preg_replace('/^\d+(.*?)\d+$/', '$1', $string);
echo "$between\n";
}
输出:
casa
a-parede-verde
sim-senhora
number10-downing-street
答案 1 :(得分:1)
如果字符串不同(即[foo]和[/ foo])
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$fullstring = '010697046a-parede-verde698012';
$parsed = get_string_between($fullstring, '010697046a', '698012');
echo $parsed; // (result = a-parede-verde)