尝试使用preg_replace对包含年份格式的字符串(添加html换行符)进行修改,例如2018
1950-2018
和1950 - 2018
$j = preg_replace('/([0-9]{4}) - ([0-9]{4})/', '<br>* ${1} - ${2}</strong>', $j);
$j = preg_replace('/([0-9]{4})-([0-9]{4})/', '<br>* ${1} - ${2}', $j);
$j = preg_replace('/\s+(19[5-9][0-9]|20(0[0-9]|10))\s+/', '<br>* ${1} </strong>', $j);
我的偏好是正则数为1950
- 2020
前两个工作正常(虽然我无法获得正确的范围),但最后一个是捕获所有实例
像:
* 2007
** 2008 - 2013
等
尝试使用^
和$
来表示开头和结尾..但第三个总是匹配前两个。
如何完全区分这些年份格式,以便我可以单独更改每一种格式?
示例代码:
<?php
$string = 'Detailed Applications: 2005-2006 Volkswagen | 2006 Volkswagen Golf 2.0L 1984CC 121Cu. In. l4 GAS SOHC Naturally Aspirated | 2005 Volkswagen Beetle 2.0L 1984CC 121Cu. In. l4 GAS DOHC Naturally Aspirated | 2005 - 2006 Volkswagen Golf';
echo $string;
echo '<br><br>';
$string = preg_replace('/([0-9]{4}) - ([0-9]{4})/', '<br /><strong>(YYYY - YYYY)* ${1} - ${2}</strong>', $string);
$string = preg_replace('/([0-9]{4})-([0-9]{4})/', '<br /><strong>(YYYY-YYYY)* ${1} - ${2}</strong>', $string);
$string = preg_replace('/(\d19[5-9][0-9]|20[0-9][0-9])(?!\s?-)/', '<br /><strong>(YYYY)* ${1} </strong>', $string);
echo $string;
输出
Detailed Applications:
(YYYY-YYYY)* 2005 -
(YYYY)* 2006 Volkswagen |
(YYYY)* 2006 Volkswagen Golf 2.0L 1984CC 121Cu. In. l4 GAS SOHC Naturally Aspirated |
(YYYY)* 2005 Volkswagen Beetle 2.0L 1984CC 121Cu. In. l4 GAS DOHC Naturally Aspirated |
(YYYY - YYYY)* 2005 -
(YYYY)* 2006
抱歉,真的很困惑。
基本上我正在做的是尝试循环它,只是做一个换行符(没有爆炸字符串)...显然,我的正则表达式输出每个都在它自己的行...
尝试获得这样的输出
Detailed Applications:
(YYYY-YYYY)* 2005 - 2006 Volkswagen |
(YYYY)* 2006 Volkswagen Golf 2.0L 1984CC 121Cu. In. l4 GAS SOHC Naturally Aspirated |
(YYYY)* 2005 Volkswagen Beetle 2.0L 1984CC 121Cu. In. l4 GAS DOHC Naturally Aspirated |
(YYYY - YYYY)* 2005 - 2006
但是啊..这是我得到的最好的
$j = preg_replace('/([0-9]{4}) - ([0-9]{4})/', '<br /><strong>* ${1} - ${2}</strong>', $j);
$j = preg_replace('/([0-9]{4})-([0-9]{4})/', '<br /><strong>* ${1} - ${2}</strong>', $j);
$j = preg_replace('/(19[5-9][0-9]|20(0[0-9]|20))(?!\s?-)/', '<br /><strong>* ${1} </strong>', $j);
所以这是我脚本的实际摘录
2007 Chevy Silverado Pickup new body style models 2008-2013 Chevy Silverado All Models 2014 Chevy Silverado 2500HD 3500HD 2007 GMC Sierra Pickup new body style models 2008-2013 GMC Sierra All Models 2014 GMC Sierra 2500HD 3500HD 2007-2013 Chevy Tahoe 2007-2013 Chevy Suburban 2007-2013 Chevy Avalanche 2007-2013 GMC Yukon Yukon XL Yukon Denali
直接上传所有一行(发布上面的示例脚本,因为此行不包含YYYY - YYYY
变体...)
它正在输出
* 2007 Chevy Silverado Pickup new body style models
* 2008 - 2013 Chevy Silverado All Models 2014 Chevy Silverado 2500HD 3500HD
* 2007 GMC Sierra Pickup new body style models
* 2008 - 2013 GMC Sierra All Models 2014 GMC Sierra 2500HD 3500HD
* 2007 - 2013 Chevy Tahoe
* 2007 - 2013 Chevy Suburban
* 2007 - 2013 Chevy Avalanche
* 2007 - 2013 GMC Yukon Yukon XL Yukon Denali
除了* 2008 - 2013 Chevy Silverado All Models 2014 Chevy Silverado 2500HD 3500HD
2014
以外的所有好处都没有突破新的一行......我也无法弄清楚如何做年份范围(doh!)甚至引用这个javascript regex validate years in range
答案 0 :(得分:1)
简化替换逻辑的一种方法是识别您希望在每4个数字年后添加<br>
,其中不是后跟短划线或空格和短跑。我们可以使用否定前瞻来轻松地说出这一点:
(\d{4})(?!\s?-)
代码示例:
$input = "that contains year formats like 2018 1950-2018 and 1950 - 2018";
echo preg_replace("/(\d{4})(?!\s?-)/", "$1<br>", $input);
that contains year formats like 2018<br> 1950-2018<br> and 1950 - 2018<br>
答案 1 :(得分:0)
preg_replace_callback()
将允许您验证/提取目标子字符串并一次性进行条件替换。
<br>
,只要它不是字符串的开头即可。第一个捕获组是\s*
,因此它将在您的目标年/年范围之前捕获零个或多个空白字符。此元素将始终以[1]
数组中的$m
存在。[2]
数组中的$m
。isset()
用于检查[3]
是否存在。\b
来确保年份值不是较大数字子串的子串,但字符串的格式允许使用空格匹配来确认准确匹配。代码:(Demo)(Pattern Demo)
$string = "2007 Chevy Silverado Pickup new body style models 2008-2013 Chevy Silverado All Models 2014 Chevy Silverado 2500HD 3500HD 2007 GMC Sierra Pickup new body style models 2008 - 2013 GMC Sierra All Models 2014 GMC Sierra 2500HD 3500HD 2007-2013 Chevy Tahoe 2007-2013 Chevy Suburban 2007 - 2013 Chevy Avalanche 2007-2013 GMC Yukon Yukon XL Yukon Denali";
echo preg_replace_callback('~(\s*)(19[5-9]\d|20[0-4]\d)(?:\s*-\s*(19[5-9]\d|20[0-4]\d))?(?=\s)~', function($m) {
//var_export($m); // un-comment if you want to see each $m array
//echo "\n---\n";
return (strlen($m[1]) ? "\n" : "")
. "<strong>*{$m[2]}"
. (isset($m[3]) ? " - {$m[3]}" : "")
. "</strong>";
},
$string);
输出:
<strong>*2007</strong> Chevy Silverado Pickup new body style models
<strong>*2008 - 2013</strong> Chevy Silverado All Models
<strong>*2014</strong> Chevy Silverado 2500HD 3500HD
<strong>*2007</strong> GMC Sierra Pickup new body style models
<strong>*2008 - 2013</strong> GMC Sierra All Models
<strong>*2014</strong> GMC Sierra 2500HD 3500HD
<strong>*2007 - 2013</strong> Chevy Tahoe
<strong>*2007 - 2013</strong> Chevy Suburban
<strong>*2007 - 2013</strong> Chevy Avalanche
<strong>*2007 - 2013</strong> GMC Yukon Yukon XL Yukon Denali