txt3rob
November 8, 2013, 10:37pm
1
im all out of ideas how to do this…
$rows = array();
foreach($trs as $tr)
$rows[] = innerHTML($tr, true);
when doing print_r($rows); i get about 10 results which is great but i need to put it all in to a string as i want to run preg match over it.
any one got any ideas how i would get it to a string so i can do preg match
cpradio
November 8, 2013, 11:11pm
2
Can you provide us with your initial input and the expected output you want?
rpkamp
November 8, 2013, 11:11pm
3
If you want a string, why are you building an array??
$str = '';
foreach($trs as $tr)
$str .= innerHTML($tr, true);
preg_match('~blabla~', $str);
txt3rob
November 8, 2013, 11:15pm
4
ill try the above as i had just copied and pasted some code that got me the results i wanted.
cheers!
cpradio
November 8, 2013, 11:15pm
5
I think that could even be more simplified:
$str = implode(' ', array_map('innerHTML', $trs)); // may require a change to innerHTML function...
txt3rob
November 8, 2013, 11:41pm
6
$scraped_page = curl("http://www.awebpage.com"); // Downloading page to variable $scraped_page
$dom = new DOMDocument();
@$dom->loadHTML($scraped_page); // Put your HTML in this variable first!
$xpath = new DOMXPath($dom);
$trs = $xpath->query('/html/body/div[3]/div/div[2]/div/div/div/div/div/div/h2');
$str = '';
foreach($trs as $tr)
$str = innerHTML($tr, true);
$returnValue = preg_match_all('#<a href="(.*?)" rel=#is', $str, $matches, PREG_PATTERN_ORDER);
$echo matches;
returns
Notice: Array to string conversion in
the line refers to a blank line after ?> where there is nothing else
txt3rob
November 8, 2013, 11:46pm
7
$trs = $xpath->query('/html/body/div[3]/div/div[2]/div/div/div/div/div/div/h2');
$str = implode(' ', array_map('innerHTML', $trs));
$returnValue = preg_match_all('#<a href="(.*?)" rel=#is', $str, $matches, PREG_PATTERN_ORDER);
echo ('<br><br><br><br><br><br>');
echo $matches;
gets
Warning: array_map(): Argument #2 should be an array in /home/site/public_html/index.php on line 42
Warning: implode(): Invalid arguments passed in /home/site/public_html/index.php on line 42
cpradio
November 9, 2013, 12:10am
8
txt3rob:
$trs = $xpath->query('/html/body/div[3]/div/div[2]/div/div/div/div/div/div/h2');
$str = implode(' ', array_map('innerHTML', $trs));
$returnValue = preg_match_all('#<a href="(.*?)" rel=#is', $str, $matches, PREG_PATTERN_ORDER);
echo ('<br><br><br><br><br><br>');
echo $matches;
gets
Warning: array_map(): Argument #2 should be an array in /home/site/public_html/index.php on line 42
Warning: implode(): Invalid arguments passed in /home/site/public_html/index.php on line 42
Ah, than you will need to use a foreach. As xpath->query doesn’t return an array.
txt3rob
November 9, 2013, 12:19am
9
so do a foreach on the $str and that should do the job?
sorry still newish at php and slowly getting there
txt3rob
November 9, 2013, 12:25am
10
i did
$xpath = new DOMXPath($dom);
$trs = $xpath->query('/html/body/div[3]/div/div[2]/div/div/div/div/div/div/h2');
foreach ($trs as $value)
$returnValue = preg_match_all('#<a href="(.*?)" rel=#is', $value, $matches, PREG_PATTERN_ORDER);
but it’s complaining it’s a object now
cpradio
November 9, 2013, 11:28am
11
txt3rob:
i did
$xpath = new DOMXPath($dom);
$trs = $xpath->query('/html/body/div[3]/div/div[2]/div/div/div/div/div/div/h2');
foreach ($trs as $value)
$returnValue = preg_match_all('#<a href="(.*?)" rel=#is', $value, $matches, PREG_PATTERN_ORDER);
but it’s complaining it’s a object now
Debug it using var_dump($value) inside your foreach. I think you’ll quickly find that is a DOMNode and you will have to use $value->nodeValue
Well how about using PHP’s ArrayObject or SplFixedArray? PHP native array is not object oriented, its not recommend to use in serious projects. The easiest way to provide string conversion from array is to subclass ArrayObject/SplFixedArray and define a __toString() method.