0

i have the below string.

$string="My Table
[table][tr][th]No.[/th][th]Content[/th][/tr][tr][td]1[/td][td]First[/td][/tr][tr][td]2[/td][td]Second[/td][/tr][tr][td]3[/td][td]Third[/td][/tr][/table]";

i want to replace this in to the following format.

$string="My Table<br><table><tr><th>No.</th><th>Content</th></tr><tr><td>1</td><td>First</td></tr><tr><td>2</td><td>Second</td></tr><tr><td>3</td><td>Third</td></tr></table>

I am trying to create a BBcode with preg_match. Please help.

I tried the following but it do not make the table tag mandatory. If some one do not close the table tag it makes issue to the whole page css.

    $string = preg_replace("#\[table\](.*?)\[\/table]#",'<table style=\'border:1px solid;\'>$1</table>',$Text);
    $Text = preg_replace("#\[tr\](.*?)\[\/tr]#is",'<tr style=\'border:1px solid;\'>$1</tr>',$Text);
    $Text = preg_replace("#\[td\](.*?)\[\/td]#is",'<td style=\'border:1px solid;padding:3px 3px 3px 3px;\'>$1</td>',$Text);
    $Text = preg_replace("#\[th\](.*?)\[\/th]#is",'<th style=\'border:1px solid;padding:3px 3px 3px 3px;\'>$1</th>',$Text);
1
  • 1
    If you're going to support BBCode (or any known format like that), wouldn't it be easier to use some already existing, and well tested, library? Things like "If some one do not close the table tag it makes issue to the whole page css", and if things can be written in different ways, can usually not be fixed by regular expressions alone (since it then would be more irregular than regular). Commented Jan 19, 2023 at 14:59

1 Answer 1

0

Actually, you just made a mistake, you need to use the same result in your next preg_replace.

Your variable $string is not reused after ...

Simply rename it to $Text

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.