简体   繁体   English

正则表达式PHP找到2个单词相同的位置

[英]Regex PHP find 2 words same position

I want to apply a regex to find the English month when i search for the month string in Spanish or other idiom. 我想用正则表达式在西班牙语或其他成语中搜索月份字符串时找到英语月份。 Currently i have an array of possible matches: 目前我有一系列可能的匹配项:

$monthsIdioms = array(
                'en' => array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'),    
'es' => array(
            '/ene/',
            '/feb/',
            '/mar/',
            '/abr/',
            '/may/',
            '/jun/',
            '/jul/',
            '/ago/',
            '/sep/',
            '/oct/',
            '/nov/',
            '/dic/'
        ));

But my date variable can have this format: 但是我的日期变量可以具有以下格式:

$date = "24 ene 15:45";

Or this one: 或者这个:

$date = "24 enero 15:45";

So i need a regex to find ene or enero in my date and with the other months: 所以我需要一个正则表达式在我的约会和其他月份中查找ene或enero:

'es' => array(
                    '/ene|enero/',
                    '/feb|febrero/',
                    '/mar|marzo/',
                    '/abr|abril/',
                    '/may|mayo/',
                    '/jun|junio/',
                    '/jul|julio/',
                    '/ago|agosto/',
                    '/set|septiembre/',
                    '/oct|octubre/',
                    '/nov|noviembre/',
                    '/dic|diciembre/'
                )

And i call this with: 我这样称呼:

preg_replace($monthsIdioms['es'], $monthsIdioms['en'], $date);

but regex | 但是正则表达式| is not properly working. 无法正常工作。 What i am dong wrong? 我错了吗?

Note that 'es' array is regex strings. 请注意,“ es”数组是正则表达式字符串。

So to clarify this, i want that if i have this date: "24 ene 15:45" or this: "24 enero 15:45" to get converted to: "24 jan 15:45" the same with with any other month of the year: "24 ago 15:45" or "24 agosto 15:45" to "24 aug 15:45". 因此,为了澄清这一点,我想如果我有这个日期:“ 24 ene 15:45”或这个日期:“ 24 enero 15:45”,将其转换为:“ 1月24日15:45”,与其他月份相同年度:“ 24前15:45”或“ 24前15:45”至“ 8月24日15:45”。

Well since all short names of the month are inside the actual name of the month you could just add a space: 好吧,由于该月的所有短名称都在该月的实际名称中,因此您只需添加一个空格即可:

'en' => array('jan ', 'feb ', 'mar ', 'etc'),
'es' => array('/ene |enero /','/feb |febrero /','/mar |marzo / ','etc')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM