使用包含文本的preg_match_all获取attr标记

问题描述 投票:1回答:3

如何获取带有图像的文本字符串中的ALT属性,使用此功能我可以获取src,但我也想获取alt

<?php 
$text = "Lorem ipsum dolor sit amet <img alt='my alt text' src='1.jpg' />  consectetur adipiscing elit, blandit quis aliquam integer neque turpis vel, ullamcorper nunc erat potenti dui vitae. Nunc tristique habitasse convallis congue praesent ultricies nam imperdiet netus, eleifend libero velit per maecenas sagittis vestibulum rhoncus felis tortor, etiam duis at sem dictumst vulputate nisi dignissim. Pharetra condimentum ultrices eleifend potenti eget blandit tempus dui class lectus magna vehicula, fames a ad ac commodo pulvinar aliquet augue semper venenatis massa, litora nascetur nam arcu et diam nec varius sollicitudin eu tristique."
preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $text, $matches);
$src = $matches[1][0]; // get attr 'src'

但是我也想要ALT,如何用类似的表达呢?

php preg-match-all
3个回答
0
投票

根据您的要求:

   $text = "Lorem ipsum dolor sit amet <img alt='my alt text' src='1.jpg' />  consectetur adipiscing elit, blandit quis aliquam integer neque turpis vel, ullamcorper nunc erat potenti dui vitae. Nunc tristique habitasse convallis congue praesent ultricies nam imperdiet netus, eleifend libero velit per maecenas sagittis vestibulum rhoncus felis tortor, etiam duis at sem dictumst vulputate nisi dignissim. Pharetra condimentum ultrices eleifend potenti eget blandit tempus dui class lectus magna vehicula, fames a ad ac commodo pulvinar aliquet augue semper venenatis massa, litora nascetur nam arcu et diam nec varius sollicitudin eu tristique.";

// array of attributes
$attr = [];

// src attribute
   preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $text, $matches);
   array_push($attr, $matches[1][0]);

// alt attribute
   preg_match_all('/<img.+?alt=[\'"]([^\'"]+)[\'"].*?>/i', $text, $matches);
   array_push($attr, $matches[1][0]);

// result
echo '<pre>';
print_r($attr);
echo '</pre>';  

1
投票

这里是带有占位符的变体,我发现它可以更轻松地用以下方法处理结果:

$re = '/(<img.*alt=\'(?<alt>.*)\'|src=\'(?<src>.*)\'.*\/>)/mU';
$str = 'Lorem ipsum dolor sit amet <img alt=\'my alt text\' src=\'1.jpg\' />  consectetur adipiscing elit, blandit quis aliquam integer neque turpis vel, ullamcorper nunc erat potenti dui vitae. Nunc tristique habitasse convallis congue praesent ultricies nam imperdiet netus, eleifend libero velit per maecenas sagittis vestibulum rhoncus felis tortor, etiam duis at sem dictumst vulputate nisi dignissim. Pharetra condimentum ultrices eleifend potenti eget blandit tempus dui class lectus magna vehicula, fames a ad ac commodo pulvinar aliquet augue semper venenatis massa, litora nascetur nam arcu et diam nec varius sollicitudin eu tristique.';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

结果:

array(2) {
  [0]=>
  array(4) {
    [0]=>
    string(22) "<img alt='my alt text'"
    [1]=>
    string(22) "<img alt='my alt text'"
    ["alt"]=>
    string(11) "my alt text"
    [2]=>
    string(11) "my alt text"
  }
  [1]=>
  array(6) {
    [0]=>
    string(14) "src='1.jpg' />"
    [1]=>
    string(14) "src='1.jpg' />"
    ["alt"]=>
    string(0) ""
    [2]=>
    string(0) ""
    ["src"]=>
    string(5) "1.jpg"
    [3]=>
    string(5) "1.jpg"
  }
}


1
投票

尝试用这种方式捕获alt和src

<?php
$re = '/<img.*?alt=\'(.*?)\' src=[\'"]([^\'"]+)[\'"].*?>/m';
$str = 'Lorem ipsum dolor sit amet <img alt=\'my alt text\' src=\'1.jpg\' />  consectetur adipiscing elit, blandit quis aliquam integer neque turpis vel, ullamcorper nunc erat potenti dui vitae. Nunc tristique habitasse convallis congue praesent ultricies nam imperdiet netus, eleifend libero velit per maecenas sagittis vestibulum rhoncus felis tortor, etiam duis at sem dictumst vulputate nisi dignissim. Pharetra condimentum ultrices eleifend potenti eget blandit tempus dui class lectus magna vehicula, fames a ad ac commodo pulvinar aliquet augue semper venenatis massa, litora nascetur nam arcu et diam nec varius sollicitudin eu tristique."
';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
print_r($matches);

?>

工作演示: https://3v4l.org/FK3nd

© www.soinside.com 2019 - 2024. All rights reserved.