search

Home  >  Q&A  >  body text

php - How to change this regex

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 .

给我你的怀抱给我你的怀抱2735 days ago894

reply all(4)I'll reply

  • 怪我咯

    怪我咯2017-06-29 10:10:37

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

    reply
    0
  • PHP中文网

    PHP中文网2017-06-29 10:10:37

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

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-29 10:10:37

    Visual inspection is that {1} is wrong

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-29 10:10:37

    , 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";
    }

    reply
    0
  • Cancelreply