PHP img url with preg_match

时间:2014-05-03 02:53:07

标签: php preg-match file-get-contents

想要只获得img url

"//lh5.googleusercontent.com/-0qGW989h9DM/AAAAAAAAAAI/AAAAAAAAG3g/aXkL8mbboKY/photo.jpg"

    <meta itemprop="name" content="Saurabh Sharma">
<meta itemprop="description" content="Works at Google as Product Manager. Lives in San Francisco. Lived in New Delhi - Mumbai - Canonsburg - Pittsburgh - Austin - San Francisco.">
<meta itemprop="image" content="//lh5.googleusercontent.com/-0qGW989h9DM/AAAAAAAAAAI/AAAAAAAAG3g/aXkL8mbboKY/photo.jpg">
<meta itemprop="url" content="https://plus.google.com/+SaurabhSharma">
</head>
<body class="Td lj">
<input type="text" name="hist_state" id="hist_state" style="display:none;">

如何使用preg_match?

1 个答案:

答案 0 :(得分:2)

要获取图像内容,请使用以下

$ data = '<meta itemprop="name" content="Saurabh Sharma"><meta itemprop="description" content="Works at Google as Product Manager. Lives in San Francisco. Lived in New Delhi - Mumbai - Canonsburg - Pittsburgh - Austin - San Francisco."><meta itemprop="image" content="//lh5.googleusercontent.com/-0qGW989h9DM/AAAAAAAAAAI/AAAAAAAAG3g/aXkL8mbboKY/photo.jpg"><meta itemprop="url" content="https://plus.google.com/+SaurabhSharma"></head><body class="Td lj"><input type="text" name="hist_state" id="hist_state" style="display:none;">';

preg_match('/<meta itemprop=\"image\" content=\"(.*?)\">/i', $data, $matches);

$ matches将返回一个数组

Array
(
    [0] => 
    [1] => //lh5.googleusercontent.com/-0qGW989h9DM/AAAAAAAAAAI/AAAAAAAAG3g/aXkL8mbboKY/photo.jpg
)