PHP preg_替换多个替换
我对正则表达式的理解很弱,这让我很头疼。我只是想从字符串中提取时间并修改时间的文本。我得到以下形式的字符串:PHP preg_替换多个替换,php,regex,preg-replace,Php,Regex,Preg Replace,我对正则表达式的理解很弱,这让我很头疼。我只是想从字符串中提取时间并修改时间的文本。我得到以下形式的字符串: "Some text I don't care about | 2.3 seconds ago" "Some text I don't care about | 5.2 minutes ago" "Some text I don't care about | 7.0 hours ago" "Some text I don't care about | 1.9 days ago" 我想将
"Some text I don't care about | 2.3 seconds ago"
"Some text I don't care about | 5.2 minutes ago"
"Some text I don't care about | 7.0 hours ago"
"Some text I don't care about | 1.9 days ago"
我想将上述字符串替换为以下格式:
"2.3 secs"
"5.2 mins"
"7.0 hrs"
"1.9 days"
我知道在正则表达式中替换的基本知识,但要删除我想要的文本前后的多个内容,而将“小时”替换为“小时”,等等超出了我的正则表达式技能水平
任何帮助都将不胜感激
谢谢
编辑:
在搞乱之后,我找到了一个使用多个不同函数的解决方案。我不太喜欢,但它很管用。如果可能的话,我更喜欢单一的preg_替换解决方案。以下是我目前的丑陋解决方案:
$testString = "This is text I don't care about | 7.3 seconds ago";
$testString = array_shift(array_reverse(explode('|', $testString)));
$pattern = array('/seconds/', '/minutes/', '/hours/', '/ago/');
$replace = array('secs', 'mins', 'hrs', '');
$testString = trim(preg_replace($pattern, $replace, $testString));
输出为:
7.3秒
preg\u replace
有一个可选的第四个参数,它是一个限制。
限制是它可以替换的最大匹配数。
Php preg_replace
它是自动全局的,这意味着它将替换所有匹配项,除非您为限制输入一个正数
preg_replace
有一个可选的第四个参数,它是一个限制。
限制是它可以替换的最大匹配数。
Php preg_replace
它是自动全局的,这意味着它将替换所有匹配项,除非您为限制输入一个正数
如果您真的希望在一行上搜索/替换,您可以执行以下操作:
$time_map = array(
'seconds' => 'secs',
'minutes' => 'mins',
'hours' => 'hrs',
'days' => 'days',
);
$data = array(
"Some text I don't care about | 2.3 seconds ago",
"Some text I don't care about | 5.2 minutes ago",
"Some text I don't care about | 7.0 hours ago",
"Some text I don't care about | 1.9 days ago",
);
foreach ($data as $line) {
$time_data = preg_replace_callback("/(.*|\s*)([0-9]+\.[0-9]+) (\w+) ago/", function ($matches) use ($time_map) {return $matches[2] . " " . $time_map[$matches[3]];}, $line);
print "$time_data\n";
}
产生:
2.3 secs
5.2 mins
7.0 hrs
1.9 days
如果您确实希望在一行上搜索/替换,可以执行以下操作:
$time_map = array(
'seconds' => 'secs',
'minutes' => 'mins',
'hours' => 'hrs',
'days' => 'days',
);
$data = array(
"Some text I don't care about | 2.3 seconds ago",
"Some text I don't care about | 5.2 minutes ago",
"Some text I don't care about | 7.0 hours ago",
"Some text I don't care about | 1.9 days ago",
);
foreach ($data as $line) {
$time_data = preg_replace_callback("/(.*|\s*)([0-9]+\.[0-9]+) (\w+) ago/", function ($matches) use ($time_map) {return $matches[2] . " " . $time_map[$matches[3]];}, $line);
print "$time_data\n";
}
产生:
2.3 secs
5.2 mins
7.0 hrs
1.9 days
使用
preg\u match
和str\u replace
功能的简单解决方案:
$testString = "This is text I don't care about | 7.3 seconds ago";
$timeUnitsmap = ["secs" => "seconds", "mins" => "minutes", "hrs" => "hours"];
preg_match("/\b[0-9.]+? \w+?\b/i", $testString, $matches);
$testString = str_replace(array_values($timeUnitsmap), array_keys($timeUnitsmap), $matches[0]);
print_r($testString); // "7.3 secs"
...
$testString = preg_replace_callback(["/.*?\b([0-9.]+? )(\w+?)\b.*/"], function($m) use($timeUnitsmap){
return $m[1]. array_search($m[2], $timeUnitsmap);
}, $testString);
或者使用preg\u replace\u回调
和array\u search
函数:
$testString = "This is text I don't care about | 7.3 seconds ago";
$timeUnitsmap = ["secs" => "seconds", "mins" => "minutes", "hrs" => "hours"];
preg_match("/\b[0-9.]+? \w+?\b/i", $testString, $matches);
$testString = str_replace(array_values($timeUnitsmap), array_keys($timeUnitsmap), $matches[0]);
print_r($testString); // "7.3 secs"
...
$testString = preg_replace_callback(["/.*?\b([0-9.]+? )(\w+?)\b.*/"], function($m) use($timeUnitsmap){
return $m[1]. array_search($m[2], $timeUnitsmap);
}, $testString);
使用
preg\u match
和str\u replace
功能的简单解决方案:
$testString = "This is text I don't care about | 7.3 seconds ago";
$timeUnitsmap = ["secs" => "seconds", "mins" => "minutes", "hrs" => "hours"];
preg_match("/\b[0-9.]+? \w+?\b/i", $testString, $matches);
$testString = str_replace(array_values($timeUnitsmap), array_keys($timeUnitsmap), $matches[0]);
print_r($testString); // "7.3 secs"
...
$testString = preg_replace_callback(["/.*?\b([0-9.]+? )(\w+?)\b.*/"], function($m) use($timeUnitsmap){
return $m[1]. array_search($m[2], $timeUnitsmap);
}, $testString);
或者使用preg\u replace\u回调
和array\u search
函数:
$testString = "This is text I don't care about | 7.3 seconds ago";
$timeUnitsmap = ["secs" => "seconds", "mins" => "minutes", "hrs" => "hours"];
preg_match("/\b[0-9.]+? \w+?\b/i", $testString, $matches);
$testString = str_replace(array_values($timeUnitsmap), array_keys($timeUnitsmap), $matches[0]);
print_r($testString); // "7.3 secs"
...
$testString = preg_replace_callback(["/.*?\b([0-9.]+? )(\w+?)\b.*/"], function($m) use($timeUnitsmap){
return $m[1]. array_search($m[2], $timeUnitsmap);
}, $testString);
要逐个替换多个字符串,还是要替换包含多个句子的单个字符串?字符串将逐个处理可以使用一个正则表达式调用一个preg_replace。要逐个替换多个字符串,或者对于包含多个句子的单个字符串?字符串将逐个处理。可以使用一个preg_replace调用和一个regex。