Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/180977-please-help-me-fix-this-regex-code/
Share on other sites

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?

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***

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

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.

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??

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.