Jump to content

Recommended Posts

Okay, i know there are lots of these questions out there and tons of ways to do this. I do have it working to replace URLs with <a href="URLs">URLs</a> but now i need some help detecting <a href=""></a> so the preg_replace will not double up the code and mess up the link.

 

here is my code so far.

<?php
echo 'COMMENTS<br /><br />';
	  while($rows = mysql_fetch_array($comres)){
		  list($comid, $menu_title, $post_id, $comdate, $comname, $comment) = $rows;
		  $comment = nl2br($comment);
		  $comment = preg_replace('/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/', '<a href="$0" target="_new">$0</a>', $comment);
		  $comment = str_replace("´", "'", $comment);
		  $comdate = date("g:ia - M j, Y",strtotime("$comdate"));
		  echo 'User: '.$comname.'<font color="#B20303"> - '.$comdate.'</font><br />';
		  echo '   - '.$comment.'<br /><br />';
	  }
?>

 

so with this code, if someone commented a link like this

 

http://example.com

 

it will turn it this

 

<a href="http://example.com" target="_new">http://example.com</a>

 

but now if someone knew some basic HTML coding, and they used this

 

<a href="http://example.com">Example.com</a>

 

it will look like this...

 

<a href=<a href="http://example.com" target="_new">http://example.com</a>>Example.com</a>

Link to comment
https://forums.phpfreaks.com/topic/258609-help-preg_replace-for-urls/
Share on other sites

Don't let them post HTML and instead allow BBCode (or something similar). People can still enter links and images, format with bold and underlines, change font size and color... It's a different yet very similar syntax, but it's so common nowadays that the people who do know HTML 99% likely know BBCode as well.

The fact of the matter is that it can be very difficult to sanitize arbitrary HTML. strip_tags() will remove tags you don't want but it won't do anything about attributes; even if you allowed only tags someone could use


You could use regular expressions to deal with most of this by making sure there aren't any invalid tags

#?b[^>]+>#

(if this matches then there's a tag with something inside it), but all you're accomplishing is allowing for BBCode tags that use s instead of []s. Which isn't bad, it's just that you've gone full circle.

 

Either way you need to do something with the comment form if you want to allow some kind of markup.

lol, yeah i noticed that... but thanks for your help, i got it all working with htmlentities(). tested it with a bunch of HTML tags and I have it set to show the tags in plain text.

 

ps. i had to scroll my text larger to read your tiny text there haha

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.