PHP REGEX: Find a dom node based on innerHTML – Education Career Blog

As I am well aware that PHPDom can solve half of my problem, I’m in need of a way (not necessarily regex) to be able to find a certain DOM element based on a given innerHTML.

say for example i got this code:

<tr>
  <td class="ranking_rank" style="vertical-align:middle;">48697</td>
  <td class="ranking_ign" style="vertical-align:middle;">kanineh</td>
  <td class="ranking_img" style="vertical-align:middle;">
    <img src="http://avatar.maplesea.com/Character/NKGEHGDLFNINKPMFLDCNNOHKHKBOHBKLGCBLABFLABHAGBPAEMDEFABJBLKJIHJAANGEKFJGELEPKMCNLKPCINEJDGAJFLKG.gif" onerror="this.src='/images/ranking/noimage.jpg'"/>
  </td>
  <td class="ranking_lvl" style="vertical-align:middle;">122</td>
  <td class="ranking_world" style="vertical-align:middle;">
    <img src="/images/ranking/Bootes.gif" onMouseover="ddrivetip('Bootes','white', 70)" onMouseout="hidetip()">
  </td>
  <td class="ranking_job" style="vertical-align:middle;">
    <img src="/images/ranking/Warrior.gif" onMouseover="ddrivetip('Warrior','white', 70)" onMouseout="hidetip()">
  </td>
  <td class="ranking_fame" style="vertical-align:middle;">449</td>
</tr>
<tr>
  <td class="ranking_rank" style="vertical-align:middle;">48698</td>
  <td class="ranking_ign" style="vertical-align:middle;">WannaLogic</td>
  <td class="ranking_img" style="vertical-align:middle;">
    <img src="http://avatar.maplesea.com/Character/DOMELFGEGCGDBFCOLADBDOJLHADCIBNKEGKGINPNBEKPDDKOEEGBLMDLBGBDHGCNPGLAECAMLGKEMDKJGPODIDKCOJCMNNKN.gif" onerror="this.src='/images/ranking/noimage.jpg'"/>
  </td>
  <td class="ranking_lvl" style="vertical-align:middle;">122</td>
  <td class="ranking_world" style="vertical-align:middle;">
    <img src="/images/ranking/Aquila.gif" onMouseover="ddrivetip('Aquila','white', 70)" onMouseout="hidetip()">
  </td>
  <td class="ranking_job" style="vertical-align:middle;">
    <img src="/images/ranking/Magician.gif" onMouseover="ddrivetip('Magician','white', 70)" onMouseout="hidetip()">
  </td>
  <td class="ranking_fame" style="vertical-align:middle;">56</td>
</tr>

I need to be able to get a hold of the whole row node with the td that has WannaLogic in it. that way, when I have this table row already, I can now easily traverse the nodes using PHP DOM. I’m a sucker for regular expression so I’d really much appreciate it if you can shed me some light on this.

,

Using regex on a DOM tree is a no-no and bound to fail when faced with malformed XML/HTML. Try this:

$xpath = new DOMXPath($doc);
$query = "//*.='WannaLogic'";
$entries = $xpath->query($query);

foreach ($entries as $entry) {
    // do whatever
}

Leave a Comment