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);
Related
This question already has answers here:
Correct syntax for hyperlink URL with PHP $_GET
(3 answers)
Closed 7 years ago.
Hope you'll be able to help me, as I searched all around Google with various keywords and couldn't find anything relevant to me, or I just don't know what this thing I'm curios about is called.
I have a website, which I want to be personalized, according to the URL string.
So I'd link to send my friend a link like http://example.com/index.php?name=Joanna and it would change the beggining of my page to "Hi Joanna!". I just want it to be simple as that and integrate it into PHP or HTML (which is easier), but don't know how to achieve that.
Any help would be appreciated :)
P.S. - Keep the "How you don't know that?" remarks away , I'm new at this and am still learning.
See HTTP GET Variables
If your URL is
www.example.com/index.php?name=Joanna
it means that $_GET["name"] is "Joanna".
So in PHP, you can output:
<?php echo "Hi ".$_GET["name"]."!"; ?>
and it will output, in this case:
Hi Joanna!
You can also check that the variable exists and isn't empty by doing one of the following:
isset($_GET["name"]) //will return true even if $_GET==""
!empty($_GET["name"])
$_GET["name"]!=""
So you can change your PHP output to:
if(!empty($_GET["name"])){
$name = $_GET["name"];
} else {
$name = "guest";
}
echo "Hi, ".$name."!";
which will output "guest" if $_GET["name"] is empty.
Shorter version of the above:
echo "Hi, ".(empty($_GET["name"]) ? "guest" : $_GET["name"])."!";
This question already has answers here:
MySQL string replace
(6 answers)
Closed 8 years ago.
I'd like to perform a find & replace in MySQL in GoDaddy's phpMyAdmin tool.
My table is called "wcswcd_posts", and I would like to find a URL, like:
http://wordpress.woodswcd.com/wordpress/wp-content/uploads/2013/11/drill.jpg
And replace it with:
/wordpress2/wp-content/uploads/2013/11/drill.jpg
Just not sure of what the syntax would be.
Thank you.
You are going to need to use the UPDATE statement & the REPLACE function
to change it try:
UPDATE wcswcd_posts SET url = REPLACE(url,'http://wordpress.woodswcd.com','')
REPLACE is a string function that replaces every occurrence of the 2nd string in the 1st string with the 3rd one.
By setting the 3rd argument in the REPLACE function to nothing, you will get rid of any occurence of http://wordpress.wordswcd.com leaving /wordpress/wp-content/uploads/2013/11/drill.jpg
This link should get you started.
Just replace http://wordpress.woodswcd.com/wordpress/ with http://wordpress.woodswcd.com/wordpress2/
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:
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
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!