Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing php to get parent element of link with URL
    text
    copied!<p>I'm trying to implement a "find and replace" system for broken links. The problem is, for some links there are no replacements. So, I need to comment out certain li elements. You can see my code below to do this. (I'm starting with an HTML form).</p> <pre><code>&lt;?php $brokenlink = $_POST['brokenlink']; $newlink = $_POST['newlink']; $brokenlink = '"' . $brokenlink . '"'; $newlink = '"' . $newlink . '"'; $di = new RecursiveDirectoryIterator('hugedirectory'); foreach (new RecursiveIteratorIterator($di) as $filename =&gt; $file) { // echo $filename . ' - ' . $file-&gt;getSize() . ' bytes &lt;br/&gt;'; $filetoedit = file_get_contents($file); if(strpos($filetoedit, $brokenlink)) { echo $brokenlink . "found in " . $filename . "&lt;br/&gt;"; $filetoedit = str_replace($brokenlink, $newlink, $filetoedit); file_put_contents($filename, $filetoedit); } } ?&gt; </code></pre> <p>What I want to accomplish is this: If I have a URL, I want to be able to find its li parent. For instance, I want PHP to be able to comment out the code below if the user inputs <a href="http://www.espn.com" rel="nofollow">http://www.espn.com</a> in an HTML form, I want php to find this element on my server:</p> <pre><code> &lt;li&gt;&lt;a href="http://www.espn.com" target="_blank" data-new="20120627"&gt;Sports&lt;/a&gt;&lt;/li&gt; </code></pre> <p>And replace it with this:</p> <pre><code> &lt;!-- &lt;li&gt;&lt;a href="http://www.espn.com" target="_blank" data-new="20120627"&gt;Sports&lt;/a&gt;&lt;/li&gt; --&gt; </code></pre> <p>Is this possible? Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload