extract info from another web page

前端 未结 2 880
借酒劲吻你
借酒劲吻你 2021-01-24 10:46

I have this test.php where i have this info :

callername1 : \'Fernando Verdasco1\'
callername2 : \'Fernando Verdasco2\'
callername3 : \'Fernando Verdasco3\'
call         


        
2条回答
  •  梦毁少年i
    2021-01-24 11:45

    You should store data in a file with the .php extension, since it's not executable PHP. I looks like you're going for the JSON syntax.

    Since you need it to work with ':' I assume, for whatever reason, you can't change the format. Your example with '=' works because of the regexp:

    preg_match_all('/callername3=\'([^\']+)\'/',$Text,$Match); 
    

    This says, match text like callername3= followed by a ' followed by one or more chars that are not a ' followed by a final '. Everything between the 's is stored in $Match[1][0] (if there were more parts in brackets they be stored in $Match[2][0], etc).

    Your example doesn't work since it doesn't account for the spaces before and after the = sign. But we can fix that up and change it to work for : like this:

    preg_match('/callername3\s*:\s*\'([^\']+)\'/',$Text,$Match); 
    echo $Match[1] ."\n"; 
    

    This displays:

    Fernando Verdasco3
    

    And what that regular expression is match text that start callername3 followed by any amount of whitespace (that's the \s*) followed by a :, followed by any amount of whitespace, followed by a name in quotes (that is stored in $Match[1], this is the area of the regular expression enclosed in parenthesis).

    I've also used just preg_match because it looks like you only need to match one example.

提交回复
热议问题