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
.
怪我咯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]中,具体信息可以打印了看
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. . . .
女神的闺蜜爱上我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";
}