-2

I know this is possible, but for the life of me I can not work it out.

Consider this scenario:

$html = '<tr>
    <td>Some Marker I know<td>
    <td>This is what I want</td>
<tr>

So a preg_match would be something like:

preg_match_all( '#Some Marker I know<td><td>(.*?)</td>#', $html, $match );

However I can't be sure of certain things, and thus need somethign which allows more flexiability

Example:

$html = '<tr class='unknown another' id='no idea'>
    <td attribute='no idea' class='no idea'>Some Marker I know<td>
    <label>This is what I want</label>
<tr>

Notice I have changed the: - Classes and ID - which may or may not exist - The html tags - which might not be TDs (might be a DIV) but will always be the next one.

So to be clear what im looking to get is the string 'This is what I want'

In english the search term would be something along the lines of:

  • Get me the contents of the next tag/element
  • Where the content of the previous element is 'Some Marker I know
  • But where the tags inbetween might (or might not) have classes, IDs and so on

I know this isnt the easiest things to example, but I was pretty sure you could use wildcards in this to help it.

Thanks!

Matt Hiscock
  • 105
  • 1
  • 7
  • It's not very clear what output you want. Is it the content of the next element after the one which has the value `Some Marker I know`, regardless of the type of element? Could you please edit your question to clarify. – Nick Oct 19 '18 at 07:57
  • Hi Nick - Ive editted it, hope that helps. – Matt Hiscock Oct 19 '18 at 08:03
  • Don't do it! You'll unleash a non-euclidean horror! https://stackoverflow.com/a/1732454/477127 – GordonM Oct 19 '18 at 08:44

1 Answers1

2

My preferred way to process HTML in PHP is using the DomDocument class. Once you've read your HTML into a DomDocument, you can then use DomXPath to search. In this case, we want to search for the element after the one which has the text value Some Marker I know. The XPath for that is

//*[text()="Some Marker I know"]/following-sibling::*

We can use that in PHP like so:

$html = "<tr class='unknown another' id='no idea'>
    <td attribute='no idea' class='no idea'>Some Marker I know<td>
    <label>This is what I want</label>
<tr>";
$doc = new DomDocument;
$doc->loadHTML($html);
$xpath = new DomXPath($doc);
$marker = $xpath->query('//*[text()="Some Marker I know"]/following-sibling::*');
echo(trim($marker[0]->nodeValue));

Output:

This is what I want

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95