html tag fun

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

html tag fun

Post 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?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

search the the string for <%tags%>.

use some regex to replace " ' " with " " ".
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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;
	}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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:
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your pattern only requires escaping the period. \.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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);
Post Reply