Here we will explain how to replace Urls with links from string
Using PHP
$string ='Rajiv Uttamchandani is an astrophysicist, human rights activist, and entrepreneur. Academy, a nonprofit organization dedicated to providing a robust technology-centered education program for refugee and displaced youth around the world.
CNN Interview - https://www.youtube.com/watch?v=EtTwGke6Jtg
CNN Interview - https://www.youtube.com/watch?v=g7pRTAppsCc&feature=youtu.be';
$string = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $string);
Using Javascript
<script>
function linkify(inputText) {
var replacedText, replacePattern1, replacePattern2, replacePattern3;
//URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
return replacedText;
}
var textTest = 'Rajiv Uttamchandani is an astrophysicist, human rights activist, and entrepreneur. He is a currently a professor of physics at Palomar College in San Marcos, CA. Additionally, he is the Founder & President of H.E.R. Academy, a nonprofit organization dedicated to providing a robust technology-centered education program for refugee and displaced youth around the world. CNN Interview - https://www.youtube.com/watch?v=EtTwGke6Jtg CNN Interview - https://www.youtube.com/watch?v=g7pRTAppsCc&feature=youtu.be';
console.log(linkify(textTest));
</script>
Using PHP
$string ='Rajiv Uttamchandani is an astrophysicist, human rights activist, and entrepreneur. Academy, a nonprofit organization dedicated to providing a robust technology-centered education program for refugee and displaced youth around the world.
CNN Interview - https://www.youtube.com/watch?v=EtTwGke6Jtg
CNN Interview - https://www.youtube.com/watch?v=g7pRTAppsCc&feature=youtu.be';
$string = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $string);
Using Javascript
<script>
function linkify(inputText) {
var replacedText, replacePattern1, replacePattern2, replacePattern3;
//URLs starting with http://, https://, or ftp://
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
return replacedText;
}
var textTest = 'Rajiv Uttamchandani is an astrophysicist, human rights activist, and entrepreneur. He is a currently a professor of physics at Palomar College in San Marcos, CA. Additionally, he is the Founder & President of H.E.R. Academy, a nonprofit organization dedicated to providing a robust technology-centered education program for refugee and displaced youth around the world. CNN Interview - https://www.youtube.com/watch?v=EtTwGke6Jtg CNN Interview - https://www.youtube.com/watch?v=g7pRTAppsCc&feature=youtu.be';
console.log(linkify(textTest));
</script>

This comment has been removed by the author.
ReplyDeleteA clear and practical guide for anyone looking to automatically convert plain URLs into clickable links. The explanation is concise, useful, and easy to follow, making this a valuable resource for improving link handling and user experience on websites.
ReplyDeleteSEO consultant Melbourne
indoor cricket Melbourne
build2 construction
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThe article presents a practical approach to converting plain URLs within a string into clickable HTML links, showing how the same requirement can be handled on both the server side and the client side. The examples demonstrate the use of regular expressions to identify URLs and transform them into anchor elements.
ReplyDeleteThe JavaScript linkify() function is particularly useful for dynamically processing text, using the replace() method and URL-matching patterns to generate clickable links. This provides a practical example for developers learning Javascript Online Course concepts such as string manipulation and regular expressions.
The PHP example complements the JavaScript implementation by showing how URL conversion can also be performed before content is rendered. Understanding both approaches helps developers recognize when text processing can be handled on the server or dynamically in the browser through Javascript Online Training.
ReplyDelete