good day!
I am currently creating a job announcement website but currently I am stuck in dealing with url links included in the content of a job announcement.
I can perfectly display the said content from my database to my website but url links that are inside that content is not detected.
In Facebook, when you post something that has url links the site automatically detect these links and I want also to achieve this in my own website.
job announcement content picture
CodePudding user response:
In A Liberal, Accurate Regex Pattern for Matching URLs I found the following Regex
\b(([\w-] ://?|www[.])[^\s()<>] (?:([\w\d] )|([^[:punct:]\s]|/)))
<?php
$str = "apply here https://ph.dbsd.com/job/dfvdfg/5444 and www.google.com also http://somesite.net";
$url_regex = "/\b((https?:\/\/?|www\.)[^\s()<>] (?:\([\w\d] \)|([^[:punct:]\s]|\/)))/";
preg_match_all($url_regex, $str, $matches);
foreach($matches[0] as $match){
$anchor = "<a href='$match'>$match</a>";
$str = str_replace($match, $anchor, $str);
}
echo $str;
// apply here <a href='https://ph.dbsd.com/job/dfvdfg/5444'>https://ph.dbsd.com/job/dfvdfg/5444</a> and <a href='www.google.com'>www.google.com</a> also <a href='http://somesite.net'>http://somesite.net</a>
tested https://3v4l.org/TqmkT