Jump to content
Sign in to follow this  
Armed Rebel

Trouble with some mark-up stuff

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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.)

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

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. :)

Share this post


Link to post
Share on other sites

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]+)?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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]1
It 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 :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
but I'd prefer it to be used with preg_replace so it won't change just one
I'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 by hacknsack

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...