gdfhghjdfghgfhf Posted November 10, 2009 Share Posted November 10, 2009 i use this code to convert html to bbcode: // Tags to Find $htmltags = array( '/\<b\>(.*?)\<\/b\>/is', '/\<i\>(.*?)\<\/i\>/is', '/\<u\>(.*?)\<\/u\>/is', '/\<ul(.*?)\>(.*?)\<\/ul\>/is', '/\<ol(.*?)\>(.*?)\<\/ol\>/is', '/\<li\>(.*?)\<\/li\>/is', '/\<img(.*?) src=\"(.*?)\" (.*?)\>/is', '/\<div\>(.*?)\<\/div\>/is', '/\<strong\>(.*?)\<\/strong\>/is', '/\<a(.*?)href(.*?)\"(.*?)\"(.*?)\>(.*?)\<\/a(.*?)\>/is', '/\<a(.*?)href(.*?)\'(.*?)\'(.*?)\>(.*?)\<\/a(.*?)\>/is', ); // Replace with $bbtags = array( '[b]$1[/b]', '[i]$1[/i]', '[u]$1[/u]', '[list]$2[/list]', '[list]$2[/list]', '[*]$1', '[img=$2]<br>', '$1', '[b]$1[/b]', '[url=$3]$5[/url]', '[url=$3]$5[/url]', ); // Replace $htmltags in $text with $bbtags $text = preg_replace ($htmltags, $bbtags, $zaqlinkclean); echo $text; it works good, but the problem is that when there are two images following each others, the result is messed up and i think there is a similar problem with link replacements Could anyone please help me to fix this ? Also, another problem (but less important) is that html tags are replaced even if empty (exemple: <a href=""></a> will still be replaced by ) thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/180977-please-help-me-fix-this-regex-code/ Share on other sites More sharing options...
cags Posted November 10, 2009 Share Posted November 10, 2009 a.) why are you escaping < and >? b.) as your using html, I'd have used a delimiter other than forward slash so you don't have to escape them either... c.) since you have the 3rd capture group set as lazy, i'd have thought it should stop after each one. Do you have an example input that causes problems? Quote Link to comment https://forums.phpfreaks.com/topic/180977-please-help-me-fix-this-regex-code/#findComment-954827 Share on other sites More sharing options...
gdfhghjdfghgfhf Posted November 10, 2009 Author Share Posted November 10, 2009 a.) why are you escaping < and >? i thought i had to.... does it really change something ? c.) since you have the 3rd capture group set as lazy, i'd have thought it should stop after each one. Do you have an example input that causes problems? this html code: <br /><br /><img src="http://i120.photobucket.com/albums/o180/seeeingred/1422573.jpg"><img src="http://i120.photobucket.com/albums/o180/seeeingred/untitled-6.jpg"><br /> returns this: [img]http://i120.photobucket.com/albums/o180/seeeingred/1422573.jpg">****REAL IMAGE HERE*** Quote Link to comment https://forums.phpfreaks.com/topic/180977-please-help-me-fix-this-regex-code/#findComment-954860 Share on other sites More sharing options...
gdfhghjdfghgfhf Posted November 10, 2009 Author Share Posted November 10, 2009 actually, here is my real input (with the problematic images): http://www.punksandskins.com/crawler1.php it is a rss feed, i'm trying to take the html content with file_get_contents, then convert the html to bbcode with the code i posted above Quote Link to comment https://forums.phpfreaks.com/topic/180977-please-help-me-fix-this-regex-code/#findComment-954886 Share on other sites More sharing options...
cags Posted November 10, 2009 Share Posted November 10, 2009 a.) why are you escaping < and >? i thought i had to.... does it really change something ? c.) since you have the 3rd capture group set as lazy, i'd have thought it should stop after each one. Do you have an example input that causes problems? this html code: <br /><br /><img src="http://i120.photobucket.com/albums/o180/seeeingred/1422573.jpg"><img src="http://i120.photobucket.com/albums/o180/seeeingred/untitled-6.jpg"><br /> returns this: [img]http://i120.photobucket.com/albums/o180/seeeingred/1422573.jpg">****REAL IMAGE HERE*** That example isn't matched by the pattern. Your pattern requires a space after the closing double bracket, which there isn't in the example. Quote Link to comment https://forums.phpfreaks.com/topic/180977-please-help-me-fix-this-regex-code/#findComment-955023 Share on other sites More sharing options...
gdfhghjdfghgfhf Posted November 10, 2009 Author Share Posted November 10, 2009 That example isn't matched by the pattern. Your pattern requires a space after the closing double bracket, which there isn't in the example. Damn, i'm an idiot! I didn,t pay attention to the space. Thanks a lot Oh and how about stop converting <a href=""></a> to would it be very hard?? Quote Link to comment https://forums.phpfreaks.com/topic/180977-please-help-me-fix-this-regex-code/#findComment-955259 Share on other sites More sharing options...
cags Posted November 10, 2009 Share Posted November 10, 2009 You could change the star in the href capture group to a + to require at least one character, that way it whould ignore empty hrefs, but I'm not sure if thats what you mean.. Quote Link to comment https://forums.phpfreaks.com/topic/180977-please-help-me-fix-this-regex-code/#findComment-955292 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.