如何在 PHP 中从 SRC 获取 ID 91


how to fetch id 91 from src in php

我从这里获取了 id 91

 <img width="526" height="339" style="float: none;" src="http://www.test.com/files/thumb/91/595" class="pyro-image" alt="floyd mayweather">

我已经使用爆炸和条形标签尝试过这个,但无法得到这个。 这是一个字符串,会根据图像不断变化。请帮忙。

如果我正确理解了您的问题,那么以下正则表达式应该适合您(注意,正则表达式和 HTML 不是朋友,但如果它不是比这更复杂,您可以使用它代替 HTML 解析器)。

#<img.*src.*/files/thumb/([0-9]+)/([0-9]+).*>#正则表达式应该有效:

<?php
    $string = '<img width="526" height="339" style="float: none;" src="http://www.test.com/files/thumb/91/595" class="pyro-image" alt="floyd mayweather">';
    preg_match('#<img.*src.*/files/thumb/([0-9]+)/([0-9]+).*>#', $string, $matches);
    echo "Image ID: " . $matches[1] . "<br />";
    echo "Other number: " . $matches[2] . "<br />";
?>

输出

Image ID: 91
Other number: 595

我假设您发布的字符串正是 - 一个字符串。考虑到这一点:

$str = '<img width="526" height="339" style="float: none;" src="http://www.test.com/files/thumb/91/595" class="pyro-image" alt="floyd mayweather">';
if (preg_match('/(?<=files'/thumb'/)[^'/'?]+/', $str, $match))
    echo $match[0]; //91

检查此代码,您可以获取所有参数 --

<?php
$html = '<img width="526" height="339" style="float: none;" src="http://www.test.com/files/thumb/91/595" class="pyro-image" alt="floyd mayweather">';
preg_match_all("/<img .*?(?=src)src='"([^'"]+)'"/si", $html, $m); 
//echo $m[1][0];
list($par1, $par2, $par3, $par4, $par5, $par6, $par7) = explode("/", $m[1][0]);
echo $par1."   ".$par2." ".$par3."   ".$par4." ".$par5."   ".$par6." ".$par7;