I have a string I need to find across thousands of files, but there is a slight difference in every line, so I need help with a Regex
The line im looking for is
<img src="images/online-chat.jpg" width="350" height="150" border="0" alt="Title Loans Antelope Valley - Online Chat"/>
And it will always be the same except the alt tag, so in the case above, "Title Loans Antelope Valley - Online Chat" is unique.
Can anyone help me with a regex that will find anything between the alt tag ""
A pattern like this should work:
alt="([^"]*)"
This will match a literal alt="
, followed by zero or more characters other than "
captured in group 1, followed by a literal "
.
preg_match('#<img src="images/online-chat.jpg" width="350" height="150" border="0" alt="(.*?)">#', $string, $matches);
The alt attribute will be in $matches[1]
.
(?<=alt=")[^"]*
this gives you the thing between alt="
and closing "
, without alt=" and "
(?:<img.+alt=")([^"]+)(?:"\/>)
Will produce:
Array
(
[0] => Array
(
[0] => <img src="images/online-chat.jpg" width="350" height="150" border="0" alt="Title Loans Antelope Valley - Online Chat"/>
)
[1] => Array
(
[0] => Title Loans Antelope Valley - Online Chat
)
)
Or for more attributes:
(?:<img\s)(?:src=")([^"]+)(?:"\swidth=")([^"]+)(?:"\sheight=")([^"]+)(?:"\sborder=")([^"]+)(?:"\salt=")([^"]+)(?:"\/>)
Will produce:
Array
(
[0] => Array
(
[0] => <img src="images/online-chat.jpg" width="350" height="150" border="0" alt="Title Loans Antelope Valley - Online Chat"/>
)
[1] => Array
(
[0] => images/online-chat.jpg
)
[2] => Array
(
[0] => 350
)
[3] => Array
(
[0] => 150
)
[4] => Array
(
[0] => 0
)
[5] => Array
(
[0] => Title Loans Antelope Valley - Online Chat
)
)
You can also use lookahead and lookbehind to pick out just the value, like so:
(?<=alt=")[^"]*(?=")
Try it:
<?php
$lines = array(
'<img src="images/online-chat.jpg" alt="Title Loans Antelope Valley - Online Chat 1"/>',
'<img src="images/online-chat.jpg" alt="Title Loans Antelope Valley - Online Chat 2"/>',
'<img src="images/online-chat.jpg" alt="Title Loans Antelope Valley - Online Chat 3"/>',
'<img src="images/online-chat.jpg" alt="Title Loans Antelope Valley - Online Chat 4"/>',
'<img src="images/online-chat.jpg" alt="Title Loans Antelope Valley - Online Chat 5"/>'
);
$alt_array = array();
foreach($lines as $line) {
$alt_array[] = getSubstring($line, 'alt="', '"');
}
print_r($alt_array);
function getSubstring($input, $start, $end)
{
preg_match("~".preg_quote($start)."(.*?)".preg_quote($end)."~", $input, $output);
return $output[1];
}
?>
Output:
Array
(
[0] => Title Loans Antelope Valley - Online Chat 1
[1] => Title Loans Antelope Valley - Online Chat 2
[2] => Title Loans Antelope Valley - Online Chat 3
[3] => Title Loans Antelope Valley - Online Chat 4
[4] => Title Loans Antelope Valley - Online Chat 5
)