php - How to change this regex
给我你的怀抱
给我你的怀抱 2017-06-29 10:08:37
0
4
838

I just started learning regular rules and tried to write one, but an error was reported. My code is:

<?php
$content = file_get_contents('page.html');
$preg='/^<meta property="og:image" content="{1}(.*)" />{1}$/';
if(preg_match($preg,$content,$arr)){ 
echo $arr[0]; 
}else{ 
echo "fail";
}
?>

The purpose is to match <meta property="og:image" content="https://scontent-nrt1-1.cdninstagram.com/t51.2885-15/e35/19428654_117834015492201_3447552780867207168_n.jpg" />The address in .

给我你的怀抱
给我你的怀抱

reply all(4)
刘奇

<?php
define('CLI_SCRIPT', true);

$content = file_get_contents('page.html');
preg_match_all('/<meta [^>]*content=\"(.+?)\"/', $content, $matches);
var_dump($matches);   // 需要的字符串在$matches[1]中,具体信息可以打印了看
Ty80

Don’t add start and end symbols. If you want to match the content, you shouldn’t add start and end symbols. . . .

曾经蜡笔没有小新

Visual inspection is that {1} is wrong

女神的闺蜜爱上我

, this is also possible. . But it’s strange, print_r and var_dump produce different results

$str = '<meta property="og:image" content="https://scontent-nrt1-1.cdninstagram.com/t51.2885-15/e35/19436259_259857144497308_9134693580806291456_n.jpg" />';

$preg='/^<meta property="og:image" content="([^\"]*)"/';
if(preg_match($preg, $str, $arr)){
    echo "<pre>";

    var_dump($arr);

    print_r($arr);

    echo $arr['1'];
}else{
    echo "fail";
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!