therocker Posted January 20, 2014 Share Posted January 20, 2014 Hello. How would you check the string for a particular word and just replace that word and not the string? I'm trying to do this so that when people just type in "google.com" or "facebook.com" or "us.gov", the site will just be linked instead of a blank text. When I tried, all the text in the string got linked and the link wasn't completed. When I viewed the page source, it was like <a href="http://google.com<br>facebook.com<br>yahoo.com<br>us.gov<br>msn.com">google.com<br>facebook.com<br>yahoo.com<br>us.gov<br>msn.com</a> The line breaks were suppose to be inserted when someone entered a new line. It should of outputted <a href="http://google.com">google.com</a><br><a href="http://facebook.com">facebook.com</a><br><a href="http://yahoo.com">yahoo.com</a><br><a href="http://us.gov">us.gov</a><br><a href="http://msn.com">msn.com</a> This is what I have <?php $row_string = $row['string_message']; if(strpos($row_string,'.com') !== false) { ?> <a href='http://<?php echo $row_string; ?>' onmousedown='return false;' target='_new'><?php echo $row_string; ?></a> <?php } elseif(strpos($row_string,'.net') !== false) { ?> <a href='http://<?php echo $row_string; ?>' onmousedown='return false;' target='_new'><?php echo $row_string; ?></a> <?php } elseif(strpos($row_string,'.org') !== false) { ?> <a href='http://<?php echo $row_string; ?>' onmousedown='return false;' target='_new'><?php echo $row_string; ?></a> <?php } elseif(strpos($row_string,'.info') !== false) { ?> <a href='http://<?php echo $row_string; ?>' onmousedown='return false;' target='_new'><?php echo $row_string; ?></a> <?php } elseif(strpos($row_string,'.gov') !== false) { ?> <a href='http://<?php echo $row_string; ?>' onmousedown='return false;' target='_new'><?php echo $row_string; ?></a> <?php } elseif(strpos($row_string,'.biz') !== false) { ?> <a href='http://<?php echo $row_string; ?>' onmousedown='return false;' target='_new'><?php echo $row_string; ?></a> <?php } elseif(strpos($row_string,'.mx') !== false) { ?> <a href='http://<?php echo $row_string; ?>' onmousedown='return false;' target='_new'><?php echo $row_string; ?></a> <?php } elseif(strpos($row_string,'.lt') !== false) { ?> <a href='http://<?php echo $row_string; ?>' onmousedown='return false;' target='_new'><?php echo $row_string; ?></a> <?php } else { echo $row_string; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/285538-php-string-replace-to-link/ Share on other sites More sharing options...
Solution Ch0cu3r Posted January 20, 2014 Solution Share Posted January 20, 2014 (edited) Your code is finding the tld within the string, such as .com .gov etc and then wrapping the whole string within an achor tag. You need to first find the domain name within the string and then warp an anchor tag around it. I'd use a regex pattern form this $string = 'I went to google.com and search for "PHP Help" and found phpfreaks.com'; // the link $replacement = '<a href="http://$1" onmousedown="return false;" target="_new">$1</a>'; // find a domain and wrap in an anchor tag $string = preg_replace('~([a-z0-9_-]+\.(com|net|org|info|gov|biz|mz|lt))~i', $replacement, $string); echo nl2br($string); Edited January 20, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285538-php-string-replace-to-link/#findComment-1465929 Share on other sites More sharing options...
therocker Posted January 20, 2014 Author Share Posted January 20, 2014 Your code is finding the tld within the string, such as .com .gov etc and then wrapping the whole string within an achor tag. You need to first find the domain name within the string and then warp an anchor tag around it. I'd use a regex pattern form this $string = 'I went to google.com and search for "PHP Help" and found phpfreaks.com'; // the link $replacement = '<a href="http://$1" onmousedown="return false;" target="_new">$1</a>'; // find a domain and wrap in an anchor tag $string = preg_replace('~([a-z0-9_-]+\.(com|net|org|info|gov|biz|mz|lt))~i', $replacement, $string); echo nl2br($string); Thanks. It worked. I've only commonly used 1 way to replace strings. I didn't know you could use preg_replace like that. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/285538-php-string-replace-to-link/#findComment-1465937 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.