0

i'm going round in circles. I want to search a string for a span tag, then replace it with an alternate version then finally print a list of all the changes.

I hope this helps show someone what i'm trying to do... fancy the challenge?

// create array to hold tooltips
$tips = array();

// search $content string and add all matching strings to $tips array
$haystack = $content;
$needle = '<span class="has-tip">['.ADD THIS TO $tips ARRAY.']</span>';

// print $content and string replace as such...
$replace = '<span class="has-tip">['.$tips[$i].']</span>';
$with = '<a href="#_ftn'.$i.'" name="_ftnref'.$i.'"><span data-tooltip aria-haspopup="true" class="has-tip top" data-disable-hover="false" tabindex="'.$i.'" title="'.$tips[$i].'">['.$i.']</span></a>';

print $content;

// list all tooltips
echo '<ul>';
foreach ($tips as $key => $tip) { 
  echo '<li><a href="#_ftnref'.$key.'" name="_ftn'.$key.'">['.$key.']</a> '.$tip.'</li>';
}
echo '<ul>';
Ben
  • 53
  • 7
  • 2
    Maybe if you could show us the data -- original input and the desired output -- we could help with the code. – Ray Paseur Jul 26 '17 at 17:01
  • Thanks Ray... http://4ipcouncil.com/footnotes.html – Ben Aug 01 '17 at 15:13
  • Any help please? – Ben Aug 02 '17 at 09:16
  • Sorry I don't have time to write up an answer, but the following should get you started on a way to solve this. https://stackoverflow.com/questions/18858493/how-to-get-span-tag-content-using-preg-match-function https://stackoverflow.com/questions/7038065/dom-change-element-content – Altimus Prime Aug 05 '17 at 02:44

1 Answers1

0

Ok, think i've got it, can anyone improve this? Thanks

// create array to hold tooltips
$tips = array();

$dom = new DOMDocument;
$dom->loadHTML($content);
foreach ($dom->getElementsByTagName('span') as $tag) {
  if ($tag->className = 'has-tip') {
    $tips[] .= $tag->nodeValue;
  }
}

$footnoted = $content;

foreach ($tips as $key => $tip){
   $i = $key + 1;
   $footnoted = str_replace('<span class="has-tip">'.$tip.'</span>', '<a href="#_ftn'.$i.'" name="_ftnref'.$i.'"><span data-tooltip aria-haspopup="true" class="has-tip top" data-disable-hover="false" tabindex="'.$i.'" title="'.$tip.'">['.$i.']</span></a>', $footnoted);
}

print $footnoted;

// list all tooltips
echo '<ul class="footnotes">';
foreach ($tips as $key => $tip) { 
  $t = $key + 1;
  echo '<li><a href="#_ftnref'.$t.'" name="_ftn'.$t.'">['.$t.']</a>&nbsp;'.$tip.'</li>';
}
echo '<ul>';
Ben
  • 53
  • 7