我正在尝试从foreach循环中获取子字符串中数字最大的结果。下面的代码返回 2 个字符串,它们只是不同,因为一个包含"220px"的子字符串,另一个包含"24px"的子字符串。我想返回为变量"220"或任何数量的子字符串中的最高数字。我使用了 http://simplehtmldom.sourceforge.net/中的"simple_html_dom.php"。任何帮助都非常感谢。
<html>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" value="google"/>
<input type="submit" value="Submit">
</form>
<?php
include 'simple_html_dom.php';
if (isset($_POST['q'])) {
$search = $_POST['q'];
$html = file_get_html("http://en.wikipedia.org/wiki/$search");
?>
<h2>Search results for '<?php echo $search; ?>'</h2>
<ol>
<?php
foreach ($html->find('img') as $element): ?>
<?php $photo = $element->src;
$logo = 'Logo';
if(strpos($photo, $logo))
{
if (preg_match_all('/[0-9]+px/', $photo, $result)) {
echo '<br/>';
$rp = trim($result[0][0],"px") .'<br/>';
echo $photo;
} else {
echo "Not found";
}
}
?>
<?php endforeach;?>
</ol>
<?php
}
?>
</body>
</html>
这里有一些你试图获取的foreach的代码。
$highest = 0;
foreach($array as $string) {
$number = intval($string);
if($number > $highest) {
$highest = $number
}
}
// highest is now the highest number gotten from the strings of the foreach loop