Armed Rebel Posted February 19, 2006 Share Posted February 19, 2006 I'm coding forums right now, and I've come to a problem with it.I'm trying to make links automatically link (so if a user typed in just "http://www.blah.com" it would link it).Here is my code, and you can view what happens here. It only gets the first letter after http://, and it messes up my sig. function markup($message){ $find = array("<", ">"); $replace = array("<", ">"); $message=str_replace($find, $replace, $message); $find=array("/\[img\](.+?)\[\/img]/", "/\[link name=(.+?)\](.+?)\[\/link\]/", "/\[link\](.+?)\[\/link\]/", "/\[b\]([^$]*)\[\/b\]/", "/\[u\]([^$]*)\[\/u\]/", "/\[i\]([^$]*)\[\/i\]/", "/\[quote\]\[originator=(.+?)\]([^$]*)\[\/quote\]/", "/\[quote\]([^$]*)\[\/quote\]/", "/\[sup\]([^$]*)\[\/sup\]/", "/\[sub\]([^$]*)\[\/sub\]/", "/\[div align=(.+?)\]([^$]*)\[\/div\]/", "/\[size=(.+?)\]([^$]*)\[\/size\]/", "/\[anchor\](.+?)\[\/anchor\]/", "/\[color=(.+?)\]([^$]*)\[\/color\]/", "/http\:\/\/(.+?)/"); $replace=array("<img src=$1>", "<a href='$2'>$1</a>", "<a href='$1'>$1</a>", "<b>$1</b>", "<u>$1</u>", "<i>$1</i>", "<br /><ul>quote <b>$1</b><br /><hr />$2<hr /></ul><br />", "<br /><ul>quote<br /><hr />$1<hr /></ul><br />", "<sup>$1</sup>", "<sub>$1</sub>", "<div align=$1>$2</div>", "<font size=\"$1\">$2</font>", "<a name=\"$1\"\></a>", "<span style=\"color:$1\">$2</span>", "<a href='http://$1'>http://$1</a>"); $message=preg_replace($find, $replace, $message); $message=nl2br($message); return $message;} Look at the last two array entries. Link to comment Share on other sites More sharing options...
Armed Rebel Posted February 20, 2006 Author Share Posted February 20, 2006 Anyone? Link to comment Share on other sites More sharing options...
hacknsack Posted February 20, 2006 Share Posted February 20, 2006 You might try reversing your handling of the http strings.(handle them first and then write your links)This is what you have in the source code (at the browser): <a href="%3Ca%20href=" http://f="">http://f</a>iles.neoseeker.com'>NeoFiles - The http's you are inserting at the first are being read later by your regular expression.Might help if you can supply a sample input to this function.(What does "$message" look like.) Link to comment Share on other sites More sharing options...
Armed Rebel Posted February 20, 2006 Author Share Posted February 20, 2006 How would I go about reversing the handling?And example message would be: omg look at this really cool site!!!1!http://www.neoseeker.com That would display omg look at this really cool site!!!1!http://www.neoseeker.com btw, When I look in the source code, I see this:<a href='<a href='http://f'>http://f</a>iles.neoseeker.com'>NeoFiles</a> Link to comment Share on other sites More sharing options...
hacknsack Posted February 20, 2006 Share Posted February 20, 2006 Added some tests to the re so that there is no re-treatment on the hyperlinks: <?php$m = '<Test This>This is a line of text[link name=Google]http://www.google.com[/link]This is a line of text[link]http://www.google.com[/link]This is a line of text[url="http://www.google.com"]http://www.google.com[/url] test';function markup($message){ $find = array("<", ">"); $replace = array("<", ">"); $message=str_replace($find, $replace, $message); $find=array("/\[img\](.+?)\[\/img]/", "/\[link name=(.+?)\](.+?)\[\/link\]/", "/\[link\](.+?)\[\/link\]/", "/\[b\]([^$]*)\[\/b\]/", "/\[u\]([^$]*)\[\/u\]/", "/\[i\]([^$]*)\[\/i\]/", "/\[quote\]\[originator=(.+?)\]([^$]*)\[\/quote\]/", "/\[quote\]([^$]*)\[\/quote\]/", "/\[sup\]([^$]*)\[\/sup\]/", "/\[sub\]([^$]*)\[\/sub\]/", "/\[div align=(.+?)\]([^$]*)\[\/div\]/", "/\[size=(.+?)\]([^$]*)\[\/size\]/", "/\[anchor\](.+?)\[\/anchor\]/", "/\[color=(.+?)\]([^$]*)\[\/color\]/", "/[^'>]http:\/\/([^'<\s]+)/"); $replace=array("<img src=$1>", "<a href='$2'>$1</a>", "<a href='$1'>$1</a>", "<b>$1</b>", "<u>$1</u>", "<i>$1</i>", "<br /><ul>quote <b>$1</b><br /><hr />$2<hr /></ul><br />", "<br /><ul>quote<br /><hr />$1<hr /></ul><br />", "<sup>$1</sup>", "<sub>$1</sub>", "<div align=$1>$2</div>", "<font size=\"$1\">$2</font>", "<a name=\"$1\"\></a>", "<span style=\"color:$1\">$2</span>", "<a href='http://$1'>http://$1</a>"); $message=preg_replace($find, $replace, $message); $message=nl2br($message); return $message;}echo markup($m);?> You'll have to test with all your inputs to make sure it works correctly. Link to comment Share on other sites More sharing options...
Armed Rebel Posted February 20, 2006 Author Share Posted February 20, 2006 That slmost works, it will only link links that have something before the http.Also, is there a place that explains, in detail, all of the different sybols? Like, ([^'<\s]+)? Link to comment Share on other sites More sharing options...
hacknsack Posted February 20, 2006 Share Posted February 20, 2006 (edited) Post the data that illustrates the problem please, I'll need it for testing.As for that re, it tests to see if the http string has already been treated by the link maker.If it has it will be surrounded by ' or > the \s makes sure we stop the capture at a space.Thanks,forgot the link:RegExLib Edited February 20, 2006 by hacknsack Link to comment Share on other sites More sharing options...
Armed Rebel Posted February 20, 2006 Author Share Posted February 20, 2006 http://www.google.com // Won't workahttp://www.google.com //will work http://test.nwtclan.net/forum/threadview.php?t=41 Link to comment Share on other sites More sharing options...
hacknsack Posted February 20, 2006 Share Posted February 20, 2006 (edited) OK, I feel like there has got to be somebody who can write a better test for this.Please don't hesitate to post a better solution if you have one.The new re captures the char before the http, so I believe it will take care of the issue. $find=array("/\[img\](.+?)\[\/img]/","/\[link name=(.+?)\](.+?)\[\/link\]/","/\[link\](.+?)\[\/link\]/","/\[b\]([^$]*)\[\/b\]/","/\[u\]([^$]*)\[\/u\]/","/\[i\]([^$]*)\[\/i\]/","/\[quote\]\[originator=(.+?)\]([^$]*)\[\/quote\]/","/\[quote\]([^$]*)\[\/quote\]/","/\[sup\]([^$]*)\[\/sup\]/","/\[sub\]([^$]*)\[\/sub\]/","/\[div align=(.+?)\]([^$]*)\[\/div\]/","/\[size=(.+?)\]([^$]*)\[\/size\]/","/\[anchor\](.+?)\[\/anchor\]/","/\[color=(.+?)\]([^$]*)\[\/color\]/","/([^'>])http:\/\/([^'<\s]+)/","/^http:\/\/([^'<\s]+)/");$replace=array("<img src=$1>","<a href='$2'>$1</a>","<a href='$1'>$1</a>","<b>$1</b>","<u>$1</u>","<i>$1</i>","<br /><ul>quote <b>$1</b><br /><hr />$2<hr /></ul><br />","<br /><ul>quote<br /><hr />$1<hr /></ul><br />","<sup>$1</sup>","<sub>$1</sub>","<div align=$1>$2</div>","<font size=\"$1\">$2</font>","<a name=\"$1\"\></a>","<span style=\"color:$1\">$2</span>","$1<a href='http://$2'>http://$2</a>","<a href='http://$1'>http://$1</a>"); Shout back if you spot a problem.OK, added a test to check if the http string starts the string...Whew.. Edited February 20, 2006 by hacknsack Link to comment Share on other sites More sharing options...
Armed Rebel Posted February 20, 2006 Author Share Posted February 20, 2006 Works good, thanks a lot. Link to comment Share on other sites More sharing options...
hacknsack Posted February 21, 2006 Share Posted February 21, 2006 You're Welcome, Link to comment Share on other sites More sharing options...
Armed Rebel Posted March 7, 2006 Author Share Posted March 7, 2006 Okay, I have a new problem, for whatever reason, nested [ul] tags (for indent) don't work.So if I were to put this code in: 1[ul]2[ul]3[/ul]2[/ul]1It would turn into:1_2_[ul]32[/ul]1 Instead of what it should be: 1_2__3_21 ("_" == indent)Here is what is being searched for, followed by what is returned. "/\[ul\]([^$]*)\[\/ul\]/U""<ul>$1</ul>" Thanks Link to comment Share on other sites More sharing options...
hacknsack Posted March 7, 2006 Share Posted March 7, 2006 You'll have to test this on your post thread, but it looks like str_replace is the ticket. <?php$tester = "1[ul]2[ul]3[/ul]2[/ul]1";$find = array("[ul]","[/ul]");$replace = array("<ul>","</ul>");echo str_replace($find, $replace, $tester);?> -hs Link to comment Share on other sites More sharing options...
Armed Rebel Posted March 9, 2006 Author Share Posted March 9, 2006 Eh, I guess that could be way, but I'd prefer it to be sued with preg_replace so it won't change just one.Thanks anyway. Link to comment Share on other sites More sharing options...
hacknsack Posted March 9, 2006 Share Posted March 9, 2006 (edited) but I'd prefer it to be used with preg_replace so it won't change just oneI'm not sure what happened for you to get that result.You can use either of these methods just adjust the arrays.str_replace(Adjust the top lines in your present markup function)$find = array("<", ">","[ul]","[/ul]");$replace = array("<", ">","<ul>","</ul>");$message=str_replace($find, $replace, $message); preg_replace(Add the search and replace strings to the arrays) $find = ........."/\[ul\]/","/\[\/ul\]/");$replace = ........."<ul>","</ul>"); -hs Edited March 9, 2006 by hacknsack Link to comment Share on other sites More sharing options...
Armed Rebel Posted March 9, 2006 Author Share Posted March 9, 2006 Well if you do a str_replace (or a preg_replace without any of the special symbols) it will change [ul] even if there is no ending for it, which I don't like. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now