2017-05-25 53 views
0

我有一些像這樣的代碼:PHP preg_replace函數返回的第一場比賽時使用(*)在中

$string = "background: #FFFFFF url('http://www.zxczxc.com/asd.jpg') 20px 100px no-repeat"; 
$value = preg_replace('/background: (.*)? ([0-9\.]+)([a-z\%]+)?/', '$2', $string); 

我怎樣才能得到preg_replace返回20px而不是100px

+0

你能分享你的預期產出嗎? –

+0

我希望返回20px – SahebSoft

+1

您想要退回或更換? – hassan

回答

2

您的(.*)?與您想要的?需要在裏面的內容相反,否則搜索是貪婪的,但可選。您還需要一個可選的前導-,因爲像素爲負值。

$value = preg_replace('/background: (.*?) (-?[0-9\.]+)([a-z\%]+)?/','$2',$string); 
+1

爲你的麻煩補償不多;-)這裏沒有兔子洞。 –

+1

@ Fred-ii-謝謝,那些wascally wabbits .. – chris85