Array to string

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

Can you provide us with your initial input and the expected output you want?

If you want a string, why are you building an array??


$str = '';
     
foreach($trs as $tr) 
   $str .= innerHTML($tr, true);

preg_match('~blabla~', $str);

ill try the above as i had just copied and pasted some code that got me the results i wanted.

cheers!

I think that could even be more simplified:

$str = implode(' ', array_map('innerHTML', $trs)); // may require a change to innerHTML function...
$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


$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.

so do a foreach on the $str and that should do the job?
sorry still newish at php and slowly getting there

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

Off Topic:

Weird that DOMNode doesn’t implement __toString(). Simply returning the nodeValue would be a perfect use of __toString methinks.

Off Topic:

I completely agree with that.

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.