簡體   English   中英

正則表達式 - 獲取字符串中的所有匹配項

[英]Regex - get all matches inside a string

我有一個像這樣的字符串:

$sContent = "t('Dashboard').' '.t('Settings Test').' '.t('My Test').' '.t('Other test')";

我需要的是一個正則表達式,它獲得每個單獨的t('')值,如:

t('Dashboard')
t('Settings Test')
...

在PHP中我用過:

$sRegExp = '/t\((.*)\)/i';
preg_match_all($sRegExp, $sContent, $aResults);

我得到與主字符串相同的結果:

t('Dashboard').' '.t('Settings Test').' '.t('My Test').' '.t('Other test')

您可以使用:

$sRegExp = '/t\((.*?)\)/

要么

$sRegExp = '/t\(([^\)]*)\)/

好吧,讓我們假設你永遠不會在那個文本中使用'。 那應該有所幫助。

preg_match_all('/t\(\'(?<=t\(\')([^\']*(?!=\'\)))\'\)/', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];

好吧,如果你想使用'符號,你必須用\\來轉義它,然后嘗試使用以下模式

/t\(\'(?<=t\(\')(((?=\\\\)\\\\\'|[^\'])*(?!=\'\)))\'\)/

我嘗試使用explode功能:

$sContent = "t('Dashboard').' '.t('Settings Test').' '.t('My Test').' '.t('Other test')";
$sContents = explode(".' '.", $sContent);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM