简体   繁体   English

正则表达式在PHP中替换为在一些前缀后得到数字或范围

[英]Regex replace in PHP to getting a number or a range after some prefixes

I have some texts with reference to lawsuit pages (folhas "fl. or fls." in portuguese) that I need to replace those numbers for a link (tag a) with an URL like: 我有一些有关诉讼页面的文本(葡萄牙语中的“ fl。or fls。”),我需要用以下网址替换链接(标签a)的这些数字:

"localhost/mni/consultarFolhas/".$page_start."/".$page_end

Text examples: 文字示例:

  • [...] Fl. [...] Fl。 53 : Considerando a manifestação do Estado, excluam-se os executados [...] 53 :考虑到一份清单,请执行[...]
  • [...] Nos termos das FLS 10/ 44 estão [...] [...] Nos termos das FLS 10/ 44estão[...]
  • [...] Ao ERJ para se manifestar objetivamente sobre fls. [...] Ao ERJ para semanar objetivamente sobre fls。 88/92 [...] 88/92 [...]
  • Às partes sobre resposta dos ofícios de fls. 单方面的法律诉讼 320/325 e fls. 320/325 e fls。 327/333 . 327/333
  • [...] observadas as verbas que constam do documento fls. daccess-ods.un.org daccess-ods.un.org作为文体的观察站 61 do [...] 61做[...]
  • Documento de Atualização de Pensão (DAP) apresentado às fls.61 , após sua contestação 第61届会议论文集 (DAP)演示文稿(apss suaCompetitionação)

As you can see some times they use plural (s) even for single pages but for multiple pages they normally use "/" to the separate the range. 如您所见,有时甚至对单个页面也使用复数,但是对于多个页面,通常使用“ /”来分隔范围。

I tried this Regex but it is still ineficiente to catch the situations described above: 我尝试了此正则表达式,但仍然不足以捕捉上述情况:

const REGEX_FOLHAS = '#^.*\bfls?\. (\d+).*$#m';

So is it possible to create a REGEX to get those situations and replace with a hyperlink like this? 那么是否可以创建一个REGEX来获取这些情况并替换为这样的超链接?

Nos termos das <a href='localhost/m-n-i/consultarFolhas/10/44/'> FLS 10/ 44 </a> estão

<a href='localhost/m-n-i/consultarFolhas/53/'>Fl. 53</a>: Considerando a manifestação do Estado, excluam-se os executados

Your regex is almost what you want but you forgot some patterns and modifiers: 正则表达式几乎是您想要的,但是您忘记了一些模式和修饰符:

  • Remove anchors and greedy dots ( m flag too) 删除锚点和贪婪的点(也为m标志)
  • Enable case-insensitivity ( i flag) 启用不区分大小写( i标志)
  • Make \\. 使\\. and following space characters optional: \\.? * 以及以下空格字符可选: \\.? * \\.? *
  • Write an optional pattern for slash separated digits (?:\\/ *\\d+)? 为斜杠分隔的数字(?:\\/ *\\d+)?编写可选模式(?:\\/ *\\d+)?
  • Use capturing groups to extract data 使用捕获组提取数据

Putting all together: 放在一起:

(?i)\bfls?\.? *(\d+)(?:(/) *(\d+))?

Live demo 现场演示

PHP code: PHP代码:

preg_replace('~(?i)\bfls?\.? *(\d+)(?:(/) *(\d+))?~', '<a href="localhost/m-n-i/consultarFolhas/$1$2$3">$0</a>');

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

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