Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to capture the text within the square brackets in the html string below. But the regex I have below doesn't get 'image' and imagealt' seperately but returns 'image]" alt="[imagealt' instead. If I take out the alt="[imagealt]" from the string it returns as I would expect/want.

$html = '<h2>[title]</h2>
<div class="content"><img src="[image]" alt="[imagealt]" /></div>
<div class="content">[text]</div>';

preg_match_all("^[(.*)]^",$html,$fields, PREG_PATTERN_ORDER);

echo "<pre>";
print_r($fields);
echo "</pre>";


Array
(
    [0] => Array
        (
            [0] => [title]
            [1] => [image]" alt="[imagealt]
            [2] => [text]
        )

    [1] => Array
        (
            [0] => title
            [1] => image]" alt="[imagealt
            [2] => text
        )

)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
662 views
Welcome To Ask or Share your Answers For Others

1 Answer

your regex is being greedy. you need to stop it being greedy to do what you want. Find out a bit more about greediness here.

When a match is greedy it will ignore the first situation which satisfies the regex and will keep trying to match until it consumes as much of the input as it can.

Usually this involves adding a ? but I'm not certain in php, but you could try:

preg_match_all("^[(.*?)]^",$html,$fields, PREG_PATTERN_ORDER);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...