在我的項目中,我通過id查找時區。如何使用正則表達式重新創建時區?
下面是一個例子:
(GMT+07:00) Indian, Christmas
我怎樣才能使它看起來像這樣:
Indian\Christmas
用正則表達式?
在我的項目中,我通過id查找時區。如何使用正則表達式重新創建時區?
下面是一個例子:
(GMT+07:00) Indian, Christmas
我怎樣才能使它看起來像這樣:
Indian\Christmas
用正則表達式?
單preg_replace()
通話將做的工作。我的模式和替換將匹配整個字符串,並將其替換爲第一個和第二個捕獲組,它們將通過\
連接在一起。
代碼:(PHP Demo)
$input='(GMT+07:00) Indian, Christmas';
echo preg_replace('/\S+ ([^,]+), (.+)/','$1\\\$2','(GMT+07:00) Indian, Christmas');
// output: Indian\Christmas
這裏是Pattern Demo
這可以有多種方式。如果您使用的是非正則表達式方法,則可以利用每個字符串前面的靜態長度:
echo str_replace(', ','\\',substr($input,12)); // same output
你可以試試這個:1)空字符串""
更換()
之間的日期,2)更換,
與\
$string = '(GMT+07:00) Indian, Christmas';
$string = preg_replace("/\([^)]+\)/","",$string);// 'Indian, Christmas'
$string = preg_replace("/\, +\)/","\\",$string);// 'Indian\Christmas'