2
A
回答
4
如果你真的想用一個正則表達式,你可以使用:
$string = "540x360 [PAR 1:1 DAR 3:2]";
$matches = array();
preg_match('/^(\d+x\d+)/i', $string, &$matches);
echo $matches[0];
4
$split = explode(' ', $string);
echo $split[0];
0
2
無需CONVER的字符串數組,可以是很煩人。
sscanf($str, "%s ", $resolution);
// $resolution = 540x360
這可以很容易地修改,以獲得分辨率的整數值:
sscanf($str, "%dx%d ", $resolution_w, $resolution_h);
// $resolution_w = 540
// $resolution_h = 360
1
<?php
function extractResolution($fromString, $returnObject=false)
{
static $regex = '~(?P<horizontal>[\d]+?)x(?P<vertical>[\d]+?)\s(?P<ignorable_garbage>.+?)$~';
$matches = array();
$count = preg_match($regex, $fromString, $matches);
if ($count === 1)
{
/*
print_r($matches);
Array
(
[0] => 540x360 [PAR 1:1 DAR 3:2]
[horizontal] => 540
[1] => 540
[vertical] => 360
[2] => 360
[ignorable_garbage] => [PAR 1:1 DAR 3:2]
[3] => [PAR 1:1 DAR 3:2]
)
*/
$resolution = $matches['horizontal'] . 'x' . $matches['vertical'];
if ($returnObject)
{
$result = new stdClass();
$result->horizontal = $matches['horizontal'];
$result->vertical = $matches['vertical'];
$result->resolution = $resolution;
return $result;
}
else
{
return $resolution;
}
}
}
$test = '540x360 [PAR 1:1 DAR 3:2] ';
printf("Resolution: %s\n", var_export(extractResolution($test, true), true));
/*
Resolution: stdClass::__set_state(array(
'horizontal' => '540',
'vertical' => '360',
'resolution' => '540x360',
))
*/
printf("Resolution: %s\n", var_export(extractResolution($test, false), true));
/*
Resolution: '540x360'
*/
相關問題
- 1. Python正則表達式解決方案?
- 2. 正則表達式沒有返回正確的解決方案
- 3. 創建正則表達式模式所需的解決方案
- 4. 正則表達式解決
- 5. 正則表達式,ANTLR還是其他解決方案?
- 6. 尋找更好的正則表達式的解決方案
- 7. 正則表達式可選空間解決方案
- 8. 正則表達式解決方案與可選
- 9. 更多正則表達式解決方案?
- 10. Python正則表達式通用解決方案
- 11. 爲www和非www域名正則表達式的解決方案
- 12. travis_c正則表達式travis_tag不解決
- 13. 如何解決與正則表達式
- 14. 正則表達式和方案
- 15. 方案正則表達式匹配
- 16. Javascript或正則表達式解決方案使標記符合XHTML標準
- 17. 找到一個更優雅的正則表達式的解決方案
- 18. 需要一個國防部重寫正則表達式解決方案.htaccess
- 19. 用替代方案和可選方案解析正則表達式
- 20. 爲什麼表達式混合打開解決方案
- 21. 解決方法缺乏向後看Javascript的正則表達式
- 22. PHP-MySQLi替換爲正則表達式/正則表達式/正則表達式
- 23. 正則表達式解釋
- 24. 解碼正則表達式
- 25. 瞭解正則表達式
- 26. C#正則表達式表達爲以重複的圖案
- 27. 正則表達式(正則表達式)
- 28. 正則表達式(正則表達式)
- 29. 正則表達式引擎如何解決不規則問題?
- 30. 正則表達式方法
親愛的向下選民,請解釋一下你爲什麼下投票我。此致,karim79。 – karim79 2011-02-09 12:57:53