Skip to content

Instantly share code, notes, and snippets.

/Index.php
Created Feb 12, 2018

Embed
What would you like to do?
<?php
function convert_to_links($msg){
$final_message = preg_replace(array('/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))/',
'/(^|[^a-z0-9_])@([a-z0-9_]+)/i', '/(^|[^a-z0-9_])#([a-z0-9_]+)/i'), array('<a href="$1" target="_blank">$1</a>', '$1<a href="index.php?user=$2">@$2</a>', '<b>$1</b><a href="index.php?hashtag=$2">#$2</a>'), $msg);
return $final_message;
}
?>
<!DOCTYPE HTML>
<html>
<title>extracting and converting #hashtags,url and @username into links in php - HackerRahul</title>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<style>
a{
color:black;
text-decoration:none;
font-weight:bold;
}
</style>
<div class='w3-container w3-blue w3-center'>
<h1>Extracting and Converting #hashtag,@username and url to links</h1>
</div>
<div class='w3-container'>
<div class='w3-content'><br>
<h4 class="w3-center"><b>Enter text with #hashtag,@username or even add links.</b></h4>
<form method="POST">
<textarea type= "text" name="text" class="w3-input" required="required"></textarea><br>
<center><input type= "submit" name="submit" class="w3-button w3-hover-blue w3-blue" value="Submit"></center><hr>
</form>
</div>
</div>
</html>
<?php
error_reporting(0);
//function to convert the url, username & hashtag into link
if(isset($_POST['submit'])){
$msg = $_POST['text'];
//dummy text you can use- Hello my name is #HackerRahul, see me on @HackerRahul . Visit my website http://hackerrahul.com
echo "<div class='w3-container'><div class='w3-content w3-border-black w3-border'><div class='w3-content w3-padding w3-blue'><h2>HackerRahul</h2></div>
<div class='w3-content w3-padding'><h4>
".convert_to_links($msg). '</h4>
</div></div></div>';
}
if($_GET['hashtag']=="" && $_GET['user']==""){
echo "<div class='w3-container'><div class='w3-content w3-border-black w3-border'><div class='w3-content w3-padding w3-blue'><h2>HackerRahul</h2></div>
<div class='w3-content w3-padding'><h4>
".convert_to_links("Hello my name is #HackerRahul, see me on @HackerRahul . Visit my website http://hackerrahul.com"). '</h4>
</div></div></div>';
exit();
}elseif($_GET['hashtag']){
echo "<div class='w3-container'><div class='w3-content'><h1>Hashtag</h1><br><h4>#". $_GET['hashtag']. "</h4></div></div>";
}else{
echo "<div class='w3-container'><div class='w3-content'><h1>Username</h1><br><h4>@". $_GET['user']. "</h4></div></div>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.