2015-02-07 24 views
0

我想重新計算CSS字符串的INT值。
PHP字符串:php重新計算css字符串的數值

$str = "background-repeat:no-repeat; position:absolute; left:522px; top:422px; width:155px; height:178px;"; 

然後我檢查每個值

$strArr= explode(";", $str); 
// output: 
Array 
(
     [0] => background-repeat:no-repeat 
     [1] => position:absolute 
     [2] => left:522px 
     [3] => top:422px 
     [4] => width:155px 
     [5] => height:178px 
     [6] => 
) 

在這裏,我想運行一個過程,更改具有PXINT值。因此,這會影響項目[2], [3], [4], [5]

我嘗試

foreach ($strArr as $piece) { 

    $item = preg_match("/\d/", $piece); // check if contains `INT` 
    if($ss==1){ 
     // run a process here to change int values 
    } 

} 
// reconstruct string 
// ex: $newStr = "background-repeat:no-repeat; position:absolute; left:261px; top:211px; width:78px; height:89px;"; 

任何幫助,將不勝感激,謝謝。

+0

有一些答案將有助於正則表達式部分。請記住,PHP具有像'preg_replace'和'preg_replace_callback'這樣的函數,這對於你想要實現的內容可能特別有意思。 – 2015-02-07 00:54:47

+0

更換不是要走的路。即時通訊尋找拉'int'值,運行一個子例程,然後創建新的字符串。 – 2015-02-07 01:04:12

+1

根據你的問題和你接受的答案,'preg_replace_callback'似乎是你想要的,但也許我誤解了。 – 2015-02-07 01:18:12

回答

1

嘗試:

$str = "background-repeat:no-repeat; position:absolute; left:522px; top:422px; width:155px; height:178px;"; 
$strArr= explode(";", $str); 
foreach ($strArr as $piece) { 

    $item = preg_match('/[0-9]+px/', $piece); // check if contains int followed by px 
    if($item==1){ 
     $piece = preg_replace('/[0-9]+px/', '400px',$piece); 
    }//here I'm replacing all the INT values to 400px 
$myValue[]=$piece; 
} 
$formattedStr = implode($myValue,';'); 
echo $formattedStr; //the result string with all the px values changed to 400 
+0

可以說我正在檢查這個'left:522px',正則表達式會給我'int val',但那麼我將如何用'left:'重新構造新值。換句話說,我需要堅持原來的財產。 – 2015-02-07 00:47:27

+0

即時通訊不理解,即時通訊不會取代任何字符串 – 2015-02-07 00:58:31

+0

你想重新計算和替換左值:522px值與左值:400px是嗎? – 2015-02-07 01:00:30

1
$str = "background-repeat:no-repeat; position:absolute; left:522px; top:422px; width:155px; height:178px;"; 

// this regex pattern will take any number that ends with px, i.e. 500px, 1px, 20px 
preg_match_all('/([0-9+])+px/', $str, $matches); 

foreach($matches[0] as $match) { 
    $intvalue = (int) $match; 
    $str = str_replace($match, '500px', $str); // subsitute 500px for what it should be replaced with 
} 

// $str is now updated.. 
+0

謝謝,但是我如何計算'500px'?我只需要在這些項目上的數字部分 – 2015-02-07 00:24:49

+1

更新,$ intvalue將是沒有「px」字符串的數字 – skrilled 2015-02-07 00:26:08

2

您將匹配更精確地使用正則表達式/(?<=:)\d+(?=px)/g,這將讓你只有數字部分,然後可以做計算。

更新,試試這個。

<?php 
$str = "background-repeat:no-repeat; position:absolute; test:0px; left:522px; top:422px; width:155px; height:178px;"; 
$strArr= explode(";", $str); 
foreach ($strArr as $pieces => $piece) { 
    $item = preg_match("/(?<=:)\d+(?=px)/", $piece, $match, PREG_OFFSET_CAPTURE); 
    if ($match) { 
     $intval = (int)$match[0][0]; 
     $offset = $match[0][1]; 
     $newVal = $intval + 100; // your calculation here 
     $strArr[$pieces] = substr($piece, 0, $offset) . $newVal . 'px'; 
    } 
} 
+0

你會知道正則表達式來抓住冒號左邊的所有內容:left:522px'(我需要'左'),這樣我可以重新創建int後已被更改的字符串? – 2015-02-07 00:54:02

+1

我編輯了答案,你可以看到這是你需要的。 – Lution 2015-02-07 01:20:37