正则表达之间的字符“

时间:2013-09-10 15:44:29

标签: php regex dreamweaver

我有一个字符串,我需要找到数千个文件,但每行都有一点点差异,所以我需要一个正则表达式的帮助

我正在寻找的线是

<img src="images/online-chat.jpg" width="350" height="150" border="0" alt="Title Loans Antelope Valley - Online Chat"/>

除了alt标签外,它总是相同的,所以在上面的例子中,“标题贷款羚羊谷 - 在线聊天”是独一无二的。

任何人都可以帮我使用正则表达式,它会在alt标签“”

之间找到任何内容

6 个答案:

答案 0 :(得分:1)

这样的模式应该有效:

alt="([^"]*)"

这将匹配文字alt=",后跟除第1组中捕获的"以外的零个或多个字符,后跟文字"

答案 1 :(得分:0)

preg_match('#<img src="images/online-chat.jpg" width="350" height="150" border="0" alt="(.*?)">#', $string, $matches);

alt属性位于$matches[1]

答案 2 :(得分:0)

   (?<=alt=")[^"]*

这为您提供了alt="closing "之间的事物,没有alt=" and "

答案 3 :(得分:0)

(?:<img.+alt=")([^"]+)(?:"\/>)

将产生:

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
        )

)

或者更多属性:

(?:<img\s)(?:src=")([^"]+)(?:"\swidth=")([^"]+)(?:"\sheight=")([^"]+)(?:"\sborder=")([^"]+)(?:"\salt=")([^"]+)(?:"\/>)

将产生:

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
        )

)

答案 4 :(得分:0)

您也可以使用lookahead和lookbehind来选择值,如下所示:

(?<=alt=")[^"]*(?=")

答案 5 :(得分:0)

试一试:

<?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];
}
?>

输出:

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
)