2

I am creating a job announcement website.

Currently, I am stuck in dealing with URLs included in the content of a job announcement.

I can perfectly display the said content from my database to my website but URLs are not detected.

In Facebook, when you post something like that, the site automatically detect these links. I want also to achieve this in my own website.

job announcement content picture

7
  • 1
    This can be a complicated problem because, on the one hand, you can skip escaping and allow the browser to render markup. But there are security issues with that. More commonly, applications allow users to use markdown to display rich text but you have to implement a markdown editor. Commented Mar 4, 2022 at 21:37
  • As it stands, your question is very vague. What does "How to automatically detect url links" actually mean? Do you want them to render as hyperlinks in the browser? Commented Mar 4, 2022 at 21:40
  • for example, I have this content: Commented Mar 4, 2022 at 23:19
  • Apply here! ph.joblum.com/job/licensing-officer-iv/1519111... ₱69,970–₱97,950 a monthFull-time Deadline for accepting applications: March 04, 2022 Commented Mar 4, 2022 at 23:19
  • I want url links to be recognized in my content, just like in facebook Commented Mar 4, 2022 at 23:21

1 Answer 1

1

In A Liberal, Accurate Regex Pattern for Matching URLs I found the following Regex

\b(([\w-]+://?|www[.])[^\s()<>]+(?:([\w\d]+)|([^[:punct:]\s]|/)))

Solution

/**
 * @param string $str the string to encode and parse for URLs
 */
function preventXssAndParseAnchors(string $str): string
{
    $url_regex = "/\b((https?:\/\/?|www\.)[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/";

    // Encoding HTML special characters To prevent XSS
    // Before parsing the URLs to Anchors
    $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');

    preg_match_all($url_regex, $str, $urls);

    foreach ($urls[0] as $url) {
        $str = str_replace($url, "<a href='$url'>$url</a>", $str);
    }

    return $str;
}

Example

<?php
$str = "
apply here https://ph.dbsd.com/job/dfvdfg/5444

<script> console.log('this is a hacking attempt hacking'); </script>

and www.google.com

also http://somesite.net
";

echo preventXssAndParseAnchors($str);

The output

apply here <a href='https://ph.dbsd.com/job/dfvdfg/5444'>https://ph.dbsd.com/job/dfvdfg/5444</a>

&lt;script&gt; console.log(&#039;this is a hacking attempt hacking&#039;); &lt;/script&gt;

and <a href='www.google.com'>www.google.com</a>

also <a href='http://somesite.net'>http://somesite.net</a>

Test https://3v4l.org/85lsl

Sign up to request clarification or add additional context in comments.

1 Comment

[\w\d]+ doesn't make sense, \d is included in \w

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.