Whitelisting urls in php
<p>we are developing a chatting application, where students and teachers can communicate via chat communication, now there are assignments in website and if a student has question related to it, he can include the url of that assignment with his message, now we want to whitelist on certain urls for security reasons for both teachers and students. </p>

<p>here how it should work</p>

<p><strong>message</strong> : question related to this assignment <a href="https://school.com/assignment/1425" rel="nofollow">https://school.com/assignment/1425</a></p>

<p>link is clickable as it is whitelisted</p>

<p><strong>message</strong> : this assignment has some issue <a href="https://schoool.com/assignment/1425" rel="nofollow">https://schoool.com/assignment/1425</a></p>

<p>this link has an extra <code>o</code> which should be marked as spam in our case, we are going to remove that link</p>

<p>we are unable to find out how we can go about this, I'm mentioning below the desired output we are expecting </p>

<p><code>https://school.com</code> <strong>whitelist</strong></p>

<p><code>https://www.school.com</code> <strong>whitelist</strong></p>

<p><code>http://school.com</code> <strong>whitelist</strong></p>

<p><code>http://wwwschool.com</code> <strong>whitelist</strong></p>

<p><code>school.com</code> <strong>whitelist</strong></p>

<p><code>www.school.com</code> <strong>whitelist</strong></p>

<p><code>www.schoool.com</code> <strong>spam url</strong></p>

<p><code>https://www.schoool.com</code> <strong>spam url</strong></p>

<p><code>www.schoool.com</code> <strong>spam url</strong></p>

<p><code>http://www.schoool.com</code> <strong>spam url</strong></p>

<p><code>schoool.com</code> <strong>spam url</strong></p>

<p><strong>our current code</strong></p>

<pre><code>function filter_url($string = null)
{
    $url = '/(((https?:\/\/)?www)?\.?[a-z0-9]+\.[a-z0-9]+[a-z0-9\-\/?&amp;#%=]+)/';
    $whitelist = '/\b(school)\b/';
    if(preg_match($url,$string,$output))
    {
        if(preg_match($whitelist,$output[0]))
        {
            // whitelisted string
            return $string;
        }
        else
        {
            return null;
        }
     }
 }
</code></pre>

<p>issue with this code is, it is whitelisting urls like</p>

<p><code>school.stealpassword.com</code></p>

<p><code>school.xxx</code></p>