Jump to content

PHP string replace to link


Go to solution Solved by Ch0cu3r,

Recommended Posts

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;
}
?>
Link to comment
https://forums.phpfreaks.com/topic/285538-php-string-replace-to-link/
Share on other sites

  • Solution

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 by Ch0cu3r

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.