html tag fun
Moderator: General Moderators
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
html tag fun
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?
Something like this:
http://php.net/preg_match
Code: Select all
if (!preg_match ("#<[^>]*'[^>]*>#", $html)):
echo '$html contains htm-tags with single quotes';
endif;- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
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;
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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.
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
feyd | It's all part of my diabolic plan! mwahahahahaha
EDIT: feyd hadn't posted yet when I checked
feyd | It's all part of my diabolic plan! mwahahahahaha
- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
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.
that dosnt work of course but how do i escape all the other characters or whatever??
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);- shiznatix
- DevNet Master
- Posts: 2745
- Joined: Tue Dec 28, 2004 5:57 pm
- Location: Tallinn, Estonia
- Contact:
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);