Page 1 of 1

html tag fun

Posted: Tue Apr 19, 2005 12:32 pm
by shiznatix
a person enteres some text and it goes to process.php. i need to know if there are any html tags in the string and if there are i need to make sure that if there is a tag that uses quotes that the quotes are double quotes not single. what would be the best way to go about this?

Posted: Tue Apr 19, 2005 12:35 pm
by Burrito
search the the string for <%tags%>.

use some regex to replace " ' " with " " ".

Posted: Tue Apr 19, 2005 12:49 pm
by vigge89
Something like this:

Code: Select all

if (!preg_match ("#<[^>]*'[^>]*>#", $html)):
	echo '$html contains htm-tags with single quotes';
endif;
http://php.net/preg_match

Posted: Tue Apr 19, 2005 1:50 pm
by shiznatix
ok i started going in a diffrent direction with this. i need to check if its a link or a img tag that it has http:// infront of it because if its just http://www.link.com then it dosnt link to the website properly. this is the only way i could think of to do it, and it works fine if there are no quotes before the tag but i need a way for it to work at all times. sorry if im being stupid.

Code: Select all

$message = str_replace("'", '"', $message);
list($one, $two, $three) = split('"', $message);

$valid = FALSE;

$check = substr($one, -9, 9);//check for <a href="
	if ($check == '<a href="'){
	$valid = TRUE;
	}
$check2 = substr($one, -10, 10);//check for <img src="
	if ($check2 == '<img src="'){
	$valid = TRUE;
	}

$beg = substr($two, 0, 7);//see if http:// was inserted
	if ($beg != 'http://'){
	$two = 'http://'.$two;
	}
	
	if ($valid == TRUE){
	$message = $one.$two.$three;
	}

Posted: Tue Apr 19, 2005 2:23 pm
by feyd
might as well call explode() on line 2 and save initializing the regular expression engine all together.. but that code seems odd though.

I'd use a preg_replace_callback() probably on that after fixing all internal quoting.

Internal quoting would also likely be a preg_replace_callback() for me; use a pattern that'll find all well formed tags. Use the callback to toss things like Javascript things.. strip the tag down to it's essentials, basically..

I find it far easier making my own tag syntax (like BBCode, maybe) this way, I control the features it'll support in the tag conversion.

Posted: Tue Apr 19, 2005 2:28 pm
by vigge89
That's something you could use regex for, check out http://php.net/preg_replace for more info ;)
EDIT: feyd hadn't posted yet when I checked :roll:



feyd | It's all part of my diabolic plan! mwahahahahaha :twisted:

Posted: Tue Apr 19, 2005 3:52 pm
by shiznatix
sorry i was being stupid i was having a monday moment. but im quite bad with regx so if you could help me with this that would be super.

Code: Select all

$patterns[0] = '/<a href="www./';
$patterns[1] = '/<img src="www./';

$replacements[1] = '/<img src="http://www./';
$replacements[0] = '/<a href="http://www./';
$message = preg_replace($patterns, $replacements, $message);
that dosnt work of course but how do i escape all the other characters or whatever??

Posted: Tue Apr 19, 2005 3:57 pm
by feyd
your pattern only requires escaping the period. \.

Posted: Tue Apr 19, 2005 4:17 pm
by shiznatix
much thanks, this worked

Code: Select all

$patterns[0] = '/<a href="www/';
$patterns[1] = '/<img src="www/';

$replacements[0] = '<a href="http://www';
$replacements[1] = '<img src="http://www';

$message = preg_replace($patterns, $replacements, $message);