1

Possible Duplicate:
bbcode unparser regex help

I want to convert this HTML code

<div class="postQuote"> <div class="postQuoteAuthor"><a href="http://www.siteurl.com/profile.php?user=Username">Username</a> wrote...</div> quoted text</div> comment 

to bbcode ..

[QUOTE=Username] quoted text [/QUOTE] comment

I don't want a tutorial about creating bbcode , it is already setup , i just want to know how to match and replace .. to make text compatible with the new board

3
  • 4
    The usual answer to HTML + regex is "Don't". stackoverflow.com/a/1732454/26702 Commented Dec 2, 2012 at 23:37
  • 1
    Wrong tool for the job. How can I convert a car into a truck using a hammer? Commented Dec 2, 2012 at 23:41
  • @Johnsyweb Chuck Norris can :) Commented Dec 3, 2012 at 1:18

2 Answers 2

0
$string = '<div class="postQuote"> <div class="postQuoteAuthor"><a href="http://www.siteurl.com/profile.php?user=Username">Username</a> wrote...</div> quoted text</div> comment ';

$string = preg_replace('|^<div class="postQuote".*user=([^"]+)".+</div>([^<]+)</div>(.+)$|', '[QUOTE=$1] $2 [/QUOTE] $3', $string);

echo $string; // [QUOTE=Username] quoted text [/QUOTE] comment 
Sign up to request clarification or add additional context in comments.

4 Comments

thank you , but it don't work as required it returns '<a href="siteurl.com/profile.php?user=username">username</a> wrote.. quoted text comment on quote ' it doesn't return '[QUOTE=Username][/QUOTE]'
If you run the above snippet it will return correctly. Please post the exact code that you are using that is causing the problem.
thank you , it worked exactly after small modification '$htmlcode = preg_replace('#<div class="postQuote".*user=([^"]+)".+</div>([^<]+)</div>#siU', '[QUOTE=$1] $2 [/QUOTE] $3', $htmlcode);'
You example did not specify it would be on multiple lines. Also 'comment' was assumed to be part of the match if you are not using that then you need to use this version so as not to be using a backreference that does not exist. '$htmlcode = preg_replace('#<div class="postQuote".*user=([^"]+)".+</div>([^<]+)</div>#siU', '[QUOTE=$1] $2 [/QUOTE]', $htmlcode);' I could have done this easier with DOM if I had known you were not needing the comment portion matched as well. I highly recommend using that instead, regex is not the ideal method to do this.
0

Well, usually people resort to preg_replace... but I prefer the lengthier stripos() process

if (($startat = stripos($html,'<div class="postQuote"'))!==false) {

  $startend = stripos($html,'</div', stripos($html,'</div', $startat)+6);

  $start_username_at = stripos($html,'<a', $startat);
  $start_username_end = stripos($html,'</a', $start_username_at);

  $start_comment_at = stripos($html,'</div', $startat)+6;
  $start_comment_end = stripos($html,'</div', $start_comment_at);

  $username = trim(strip_tags(substr($html,$start_username_at,$start_username_end-$start_username_at)));
  $comment = trim(strip_tags(substr($html,$start_comment_at,$start_username_end-$start_comment_at)));
  echo "[QUOTE=Username]".$username."[/QUOTE] ".$comment;

}

2 Comments

This is untested and wrote it quick, let me know if it doesn't work or if you want me to explain it
Now it has been edited and tested :) sorry

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.