This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: STR replace by link
i'm trying to make a website that has a chatbox or a shoutbox..
when i enter a link on the textarea, for example http://link.com/ and submit it, it appears just a plain text on the chatbox, how can i make it like facebook chat any other chat that when you enter a link it will display as a link with the underline that you can click.. and i don't know how to do it..
RegEx and preg_replace are a good start for this:
$pattern = "/http:\/\/(www\.)?([^.]+\.[^.\s]+\.?[^.\s]*)/i";
$replace = "<a href='http://\\1\\2'>http://\\1\\2</a>";
$string = preg_replace($pattern,$replace,$string);
echo $string;
The good point here is, you can type in something like:
Hello, look at my site http://www.google.de
Which will be translated into:
Hello, look at my site http://www.google.de
Related
This question already has answers here:
How do I run PHP code when a user clicks on a link?
(10 answers)
Closed 9 years ago.
Here is the code:
$vote_up = 'Vote Up';
I am simply trying to have my PHP function insert a link that has a line of PHP as the href. But it does not work right. How can I do this?
Quick update: I got some good answers, however I'd like for the function to fire when clicked (hence the need for the <?php tags).
You're trying to use PHP tags inside the string context. They have no meaning and hence won't produce the output you need.
Simply use string concatenation:
$vote_up = 'Vote Up';
Or use sprintf() (bit more cleaner):
$vote_up = sprintf('Vote Up', amc_comment_vote("up"));
this is also a possibility:
$vote_up = "<a href='".amc_comment_vote("up")."'>Vote Up</a>";
This question already has an answer here:
PHP: Properly convert addresses to clickeable links in string
(1 answer)
Closed 9 years ago.
$string1 = "You can view the full calendar at
http://www.something.org/calendar/57682117
or email: info#something.org"
$string2 = "You can view the full calendar at
www.something.org/calendar/57682117
or email: info#something.org"
This is the string(s) I have. What I need to do is convert the two urls to links that will link to a page, and then the emails will need to be converted to mailto: links.
I don't need to account for websites that are blahblah.com. They will always have one of these www. or http(s). I am terrible with regular expressions... so help will be appreciated!
$output = preg_replace('_<a href="(?!mailto:)(?!tel:)[^"]+[^>]*>(.+?)</a>_', '$1', $string1);
echo '<pre>'.print_r(htmlentities($output), true).'</pre>';
This question already has answers here:
Replace URLs in text with HTML links
(17 answers)
Closed 9 years ago.
I have a input field which user enters something. Basically when user enter an url which start with http:// https// or www. i want to store it like user's words...
I'm working with php and mysql. I think replace function is for it. But when I read about it, this function only working on all string as far as I know.
For example:
input:
Hi, how are you? Please check this link : http:// www.google.com and enjoy.
output should like below:
Hi, how are you? Please check this link : http://www.google.com and enjoy.
Try this: (I don't test this code)
$output = preg_replace("#(http://|www\.)(.*) #Uis",'$1$2', $input);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Linkify PHP text
Hi I have a field from a database that contains http links as well as normal text.
I need to find out a way to display this information using PHP to show Text and a link when applicable.
the following is my sql query:
$sql =
"select bw_supporting_doc
from cm3rm2 with(nolock)
where number = '$id'";
the output of this sql statement is:
Please find below the link to the FSS: http://blahblack.html
I need to display the http://blahblack.html as a link at the moment it is displaying as normal text.
Thanks!
function hyperlink($text){
$text = preg_replace("#(http://)+?([.]?[a-zA-Z0-9_/-])*#", "\\0", $text);
return $text;
}
this might do the job
example usage:
$str = 'this is a random string which contains a link http://www.yahoo.com';
echo hyperlink($str);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Detecting a url using preg_match? without in the string
I wanting to search a string that is input by the user, for instances of http:// if one is found I want to wrap the link in the correct html,
so for example, the following could be input by a user,
Google's url is http://www.google.com,
what I want to save that as it,
Google's url is http://www.google.com
is this possible?
function make_clickable($sText) {
$sClickText = str_replace(' www.', ' http://www.', $sText);
$sClickText = preg_replace("/([\s])?(http|ftp|https)([\:\/\/])([^\s]+)/i", " $2$3$4",$sClickText);
return $sClickText;
}
Tadaa!