浏览文章

文章信息

正则匹配标签所有属性 170

正则匹配所有img再匹配所有的img属性

public static function getContentImageElementAttributes($content, $order = 'ALL') { $pattern = '/<img[^>]+>/'; preg_match_all($pattern, $content, $matches); $elseAttributes = []; if (!empty($matches[0])) { foreach ($matches[0] as $imgTag) { $attrPattern = "/\s+([a-zA-Z0-9-]+)\s*=\s*[\"']([^\"']*)[\"']/"; preg_match_all($attrPattern, $imgTag, $attrMatches); if (!empty($attrMatches[1]) && !empty($attrMatches[2])) { $attributes = array_combine($attrMatches[1], $attrMatches[2]); $attributes['tag'] = $imgTag; $elseAttributes[] = $attributes; } } } return $elseAttributes; }

$attrPattern = "/\s+([a-zA-Z0-9-]+)\s*=\s*[\"']([^\"']*)[\"']/";

原创