tags:

views:

43

answers:

2

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.

A: 

I did this:

<?php
$data = 'scrapehere<input type="hidden" id="_someid" name="_somename" value="value"/>';
$regex = '/scrapehere<input type="hidden" id="_someid" name="_somename" value="value"/';
preg_match($regex,$data,$match);
print_r($match);
echo $match[1];
?>

I get this:

Array
(
    [0] => scrapehere<input type="hidden" id="_someid" name="_somename" value="value"
)

Exactly what I was expecting. What are the contents of your scrape-test.html file?

CRasco
it has form and inside this form there is this field: scrapehere<input type="hidden" id="_someid" name="_somename" value="value"/>
Phil
+1  A: 
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.

SilentGhost
wow, works perfectly! thanks
Phil