Liam Fell
Liam Fell

Reputation: 1320

Concatenating an xPath query in PHP

I'm using xPath to query a HTML document. I'm setting a string as the contents of the xPath query. So in this case '$category_titles' is set to the the text that the query returns.

$category_titles = $xpath->query('//*[@id="test"]/div/div/h1');

Is there a way to pass in the values of two xpath queries in to my string though? So effectively, I need to set $category_titles to the result of two separate xPath values.

Upvotes: 1

Views: 391

Answers (2)

Darko Miletic
Darko Miletic

Reputation: 1356

You should use operator | to merge more than one different paths into one. this is valid:

$titles = $xpath->query('//*[@id="test"]/div/div/h1 | //*[@id="test1"]/div/div/h1');

Upvotes: 1

James
James

Reputation: 1769

I don't know if I understood what you want exactly, but if you are trying to have two xpath results in the same variable, the only way is using a array.

$category_title = array();
$category_titles[0] = $xpath->query('//*[@id="test"]/div/div/h1');
$category_titles[1] = $xpath->query('//*[@id="test1"]/div/div/h1');//I changed the ID, but you can change your entire query

Then, if you want to concatenate it, just a $concat = "$category_titles[0]" . "$category_titles[1]" is enough.

However, if you are trying create a second query using the result of first one and then concatenate, you can do:

$category_titles = $xpath->query('//*[@id="test"]/div/div/h1');
$category_titles = "$category_titles" . $xpath->query("//*[@id=\"$category_titles\"]/div/div/h1");// Note the escaped slashes

Upvotes: 0

Related Questions