开发者

Simple preg_match question

The following regex gets all images src but shows it with src=" at the beginin开发者_运维问答g and " at the end of the path.

How can I remove it and show only the path?

preg_match('/src="(.*?)"/i' , $content1 , $matches);


You're almost doing it right. The regular expression is correct, but:

echo $matches[1];

This will output the first captured subpattern. The first match ($matches[0]) will always contain the full text that was matched, that is including the src="" bits.


Code:

$re = '/(?<=src=")(?:.*?)(?=")/ui';
$txt = '<img src="http://www.gravatar.com/avatar/6f5de9?s=32&amp;d=identicon&amp;r=PG" height="32" width="32" alt="">';

$nMatches = preg_match($re, $txt, $aMatches);

Output:

Array
(
    [0] => http://www.gravatar.com/avatar/6f5de9?s=32&d=identicon&r=PG
)


$matches will contain the entire matched pattern in the first element, followed by elements for each subpattern. In this case, you want $matches[1] for the stuff you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜