Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GetImgSrc.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: https://github.com/ITzhiwei/getImgSrc
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 获取HTML字符串中的图片地址类
  13. // +----------------------------------------------------------------------
  14. namespace util;
  15. class GetImgSrc
  16. {
  17. /**
  18. * 提取HTML文章中的图片地址
  19. * @param string $data HTML或者文章
  20. * @param int $num 第 $num 个图片的src,默认为第一张
  21. * @param string $order 顺取倒取; 默认为 asc ,从正方向计数。 desc 从反方向计数
  22. * @param string|array $blacklist 图片地址黑名单,排除图片地址中包含该数据的地址;例如 传入 baidu.com 会排除 src="http://www.baidu.com/img/a.png"
  23. * @param string $model 默认为字符串模式;可取值 string preg;string模式处理效率高,PHP版本越高速度越快,可比正则快几倍
  24. * @return false| null | src 当data为空时返回 false , src不存在时返回 null ,反之返回src
  25. */
  26. public static function src($data, $num = 1, $order = 'asc', $blacklist = false, $model = 'string')
  27. {
  28. if (isset($data)) {
  29. if ($model === 'preg') {
  30. $imgSrc = self::pregModel($data, $num - 1, $order);
  31. } else {
  32. $imgSrc = self::strModel($data, $num, $order);
  33. }
  34. if ($blacklist === false) {
  35. return $imgSrc;
  36. } else {
  37. if (is_array($blacklist)) {
  38. foreach ($blacklist as $value) {
  39. if (strpos($imgSrc, $value) !== false) {
  40. return self::src($data, $num + 1, $order, $blacklist, $model);
  41. };
  42. }
  43. return $imgSrc;
  44. } else {
  45. if (false === $blacklist || strpos($imgSrc, $blacklist) === false) {
  46. return $imgSrc;
  47. } else {
  48. return self::src($data, $num + 1, $order, $blacklist, $model);
  49. }
  50. }
  51. }
  52. } else {
  53. return false;
  54. }
  55. }
  56. /**
  57. * 提取HTML文章中的图片地址
  58. * @param string $data HTML或者文章
  59. * @param int $startNum 默认为1,从第一张图片开始抽取
  60. * @param int $length 从 $startNum 开始抽取,共抽取 $length 张;默认为0,为0则抽取到最后
  61. * @param string $order 顺取倒取; 默认为 asc ,从正方向计数。 desc 从反方向计数
  62. * @param string|array $blacklist 图片地址黑名单,排除图片地址中包含该数据的地址;例如 传入 img.baidu.com 会排除 src="img.baidu.com/a.png"
  63. * @param string $model 抽取集合时,默认为正则模式;可选模式:preg string,当 $length > 3 或者 $length = 0时,强制使用正则模式,因为取的数量大时,正则速度更快。
  64. * @return array 图片地址的集合数组,若无则返回空数组
  65. */
  66. public static function srcList($data, $startNum = 1, $length = 0, $order = 'asc', $blacklist = false, $model = 'preg')
  67. {
  68. if ($model === 'preg' || $length > 3 || $length === 0) {
  69. $imgSrcArr = self::pregModel($data, [$startNum - 1, $length, $blacklist], $order);
  70. } else {
  71. $imgSrcArr = [];
  72. for ($i = $startNum; $i < $startNum + $length; $i++) {
  73. $imgSrc = self::strModel($data, $i, $order);
  74. if (is_array($blacklist)) {
  75. $blackBool = true;
  76. foreach ($blacklist as $k => $v) {
  77. if (strpos($imgSrc, $blacklist) !== false) {
  78. $blackBool = false;
  79. }
  80. }
  81. if ($blackBool) {
  82. $imgSrcArr[] = $imgSrc;
  83. } else {
  84. $length++;
  85. }
  86. } else {
  87. if (false === $blacklist || strpos($imgSrc, $blacklist) === false) {
  88. $imgSrcArr[] = $imgSrc;
  89. } else {
  90. $length++;
  91. }
  92. }
  93. }
  94. }
  95. return $imgSrcArr;
  96. }
  97. /**
  98. * @param $str
  99. * @param $num
  100. * @param $order
  101. * @return bool|string|null
  102. */
  103. public static function strModel($str, $num, $order)
  104. {
  105. $topStr = null;
  106. if ($order != 'asc') {
  107. $funcStr = 'strrpos';
  108. } else {
  109. $funcStr = 'strpos';
  110. }
  111. for ($i = 1; $i <= $num; $i++) {
  112. $firstNum = $funcStr($str, '<img');
  113. if ($firstNum !== false) {
  114. if ($order != 'asc') {
  115. $topStr = $str;
  116. $str = substr($str, 0, $firstNum);
  117. } else {
  118. $str = substr($str, $firstNum + 4);
  119. }
  120. } else {
  121. return null;
  122. }
  123. }
  124. $str = $order == 'asc' ? $str : $topStr;
  125. $firstNum1 = $funcStr($str, 'src=');
  126. $type = substr($str, $firstNum1 + 4, 1);
  127. $str2 = substr($str, $firstNum1 + 5);
  128. if ($type == '\'') {
  129. $position = strpos($str2, "'");
  130. } else {
  131. $position = strpos($str2, '"');
  132. }
  133. $imgPath = substr($str2, 0, $position);
  134. return $imgPath;
  135. }
  136. /**
  137. * @param $str
  138. * @param $num
  139. * @param $order
  140. * @return string|array|null
  141. */
  142. public static function pregModel($str, $num, $order)
  143. {
  144. preg_match_all("/<img.*>/isU", $str, $ereg);
  145. $img = $ereg[0];
  146. if ($order != 'asc') {
  147. $img = array_reverse($img);
  148. };
  149. if (is_array($num)) {
  150. $startNum = $num[0];
  151. $length = $num[1];
  152. $blacklist = $num[2];
  153. $imgSrcArr = [];
  154. foreach ($img as $key => $value) {
  155. $imgSrc = $value;
  156. $pregModel = '/src=(\'|")(.*)(?:\1)/isU';
  157. preg_match_all($pregModel, $imgSrc, $img1);
  158. if (is_array($blacklist)) {
  159. $blacklistBool = true;
  160. foreach ($blacklist as $v) {
  161. if (strpos($img1[2][0], $v) !== false) {
  162. $blacklistBool = false;
  163. };
  164. }
  165. if ($blacklistBool) {
  166. $imgSrcArr[] = $img1[2][0];
  167. };
  168. } else {
  169. if (false === $blacklist || strpos($img1[2][0], $blacklist) === false) {
  170. $imgSrcArr[] = $img1[2][0];
  171. };
  172. }
  173. }
  174. if ($length > 0) {
  175. return array_slice($imgSrcArr, $startNum, $length);
  176. } else {
  177. return array_slice($imgSrcArr, $startNum);
  178. }
  179. } else {
  180. if (!empty($img[$num])) {
  181. $imgStr = $img[$num];
  182. $pregModel = '/src=(\'|")(.*)(?:\1)/isU';
  183. preg_match_all($pregModel, $imgStr, $img1);
  184. return $img1[2][0];
  185. } else {
  186. return null;
  187. }
  188. }
  189. }
  190. }