获取包含文本的 preg_match_all 的 attr 标签
Get attr tag with preg_match_all that contains a text
如何在带有图像的文本字符串中获取 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,请问类似的表达式怎么办?
根据您的要求:
$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>';
试试这种方式获取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
这是一个带有占位符的变体,我发现它可以更轻松地处理结果:
$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"
}
}
如何在带有图像的文本字符串中获取 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,请问类似的表达式怎么办?
根据您的要求:
$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>';
试试这种方式获取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
这是一个带有占位符的变体,我发现它可以更轻松地处理结果:
$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"
}
}