iyeru42 Posted September 23, 2007 Share Posted September 23, 2007 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 More sharing options...
smiles Posted September 24, 2007 Share Posted September 24, 2007 try this preg_replace("((www|http)(\W+\S+[^).,:;?\]\} \r\n$]+))", "<a href='$1' target='_blank'>$1</a>", $yourVariable); Link to comment Share on other sites More sharing options...
iyeru42 Posted September 25, 2007 Author Share Posted September 25, 2007 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 More sharing options...
justsomeguy Posted September 25, 2007 Share Posted September 25, 2007 There's an extra ) at the end of the pattern. Link to comment Share on other sites More sharing options...
smiles Posted September 26, 2007 Share Posted September 26, 2007 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 More sharing options...
iyeru42 Posted September 26, 2007 Author Share Posted September 26, 2007 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 More sharing options...
justsomeguy Posted September 26, 2007 Share Posted September 26, 2007 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 More sharing options...
iyeru42 Posted September 27, 2007 Author Share Posted September 27, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.