Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

php - scrape & regex match doesn't work

Ok, I have page with something like this (I added 'scrapehere' string to make it easier to navigate, this page isn't 100% correct html and it has two identical fields with different values. No, I can't fix it because it's cms i'm using and i feel it would be too complicated for me to do):

scrapehere<input type="hidden" id="_someid" name="_somename" value="value"/>

I'm trying to get hidden value. So I wrote such script:

<?php
$data = file_get_contents('scrape-test.html');
$regex = '/scrapehere<input type="hidden" id="_someid" name="_somename" value="(.+?)"/';
preg_match($regex,$data,$match);
var_dump($match);
echo $match[1];
?>

But instead of my value script outputs this:

array(2) { [0]=>  string(74) "scrapehere  string(5) "value" } value

What's wrong with it, why won't it just print value? Did it already saved it somewhere but my echo is wrong? I want output to be just value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
var_dump($match);
echo $match[1];

both of these lines output data. var_dump outputs an array first element of which contains an input tag, which is not displayed in the browser because it's hidden!

so, if you want output to be only 'value', remove var_dump($match); from your code and let the echo do the job.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...