Jump to content

preg_replace word that starts with...


iyeru42
 Share

Recommended Posts

Can someone help me out with this? I don't know patterns, so I've come here to ask how to define the pattern to do as such. Although I'd rather have the entire preg_replace done for me.Anyway, here's what I want: I want to preg_replace an entire word that starts with http:// and replace it with an <a href="(matched word)">(matched word)</a>

Link to comment
Share on other sites

try this :)
preg_replace("((www|http)(\W+\S+[^).,:;?\]\} \r\n$]+))", "<a href='$1' target='_blank'>$1</a>", $yourVariable);

Gives me:
Warning: preg_replace() [function.preg-replace]: Unknown modifier ')' in preg_replace.php

And using str_replace doesn't replace anything with your regex with the string:

$text = <<<TEXTENDthats not what he wanted, he wanted to replace http://krakjoe.com with a link to my website. And http://www.freewebspace.net and maybe http://www.markforums.com or http://www.gaiaonline.com.TEXTEND;

And PLEASE don't leave me hanging. I get far too much of topics with no replies everywhere I go.

Link to comment
Share on other sites

sorry, I forgot sign "@"like this

preg_replace("@((www|http)(\W+\S+[^).,:;?\]\} \r\n$]+))@", "<a href='$1' target='_blank'>$1</a>", $yourVariable);

or this for sure

$pattern = "@((www|http)(\W+\S+[^).,:;?\]\} \r\n$]+))@";$replace = "<a href='$1' target='_blank'>$1</a>";preg_replace($pattern, $replace, $yourVariable);

:)

Link to comment
Share on other sites

When using the following code smiles has:

<?php$yourVariable = "thats not what he wanted, he wanted to replace http://krakjoe.com with a link to my website. And http://www.freewebspace.net and maybe http://www.markforums.com or http://www.gaiaonline.com."$pattern = "@((www|http)(\W+\S+[^).,:;?\]\} \r\n$]+))@";$replace = "<a href='$1' target='_blank'>$1</a>";preg_replace($pattern, $replace, $yourVariable);echo $yourVariable;?>

It gives me:

Parse error: syntax error, unexpected T_VARIABLE in preg_replace.php on line 4

Also, using the TEXTEND won't work with smiles preg_replace.

Link to comment
Share on other sites

You're missing a semicolon. If you want to use heredoc syntax you can.

<?php$yourVariable = <<<EOTthats not what he wanted, he wanted to replace http://krakjoe.com with a link to my website. And http://www.freewebspace.net and maybe http://www.markforums.com or http://www.gaiaonline.com.EOT;$pattern = "@((www|http)(\W+\S+[^).,:;?\]\} \r\n$]+))@";$replace = "<a href='$1' target='_blank'>$1</a>";preg_replace($pattern, $replace, $yourVariable);echo $yourVariable;?>

Link to comment
Share on other sites

You're missing a semicolon. If you want to use heredoc syntax you can.
<?php$yourVariable = <<<EOTthats not what he wanted, he wanted to replace http://krakjoe.com with a link to my website. And http://www.freewebspace.net and maybe http://www.markforums.com or http://www.gaiaonline.com.EOT;$pattern = "@((www|http)(\W+\S+[^).,:;?\]\} \r\n$]+))@";$replace = "<a href='$1' target='_blank'>$1</a>";preg_replace($pattern, $replace, $yourVariable);echo $yourVariable;?>

Nothing gets replaced with that code: it shows up all as text still (shown here) However, the below will work:
	if( preg_match_all( "~(https?|ftp)://(.*?[^ ]+)~si", $text, $links ) )	{		foreach( $links[0] as $id => $url )		{			$search[ ] =  $url;			replace[ ] = sprintf( "<a href=\"%s://%s\">%s://%s</a> ", $links[1][$id], $links[2][$id], $links[1][$id], $links[2][$id] );		}		return str_replace( $search, $replace, $text );	}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...