【问题标题】:PHP img url with preg_matchPHP img url 与 preg_match
【发布时间】:2014-05-03 02:53:07
【问题描述】:

只想获取 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 做到这一点?

【问题讨论】:

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


    【解决方案1】:

    为了获取图片内容,使用如下

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

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

    $matches 将返回一个数组

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

    【讨论】:

    • 内容和 itemprop 应该被翻转然后它应该可以工作
    • print_r($matches[1]) 会给你图片网址
    • 有没有办法在结果前添加http:?
    猜你喜欢
    • 2014-03-23
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多