-2

for my website, I want a simple BB code system. Nothing special--just hyperlinks and images will be fine for now.

I'm not good with RegExp. Period. But if someone could show me an example, I may be able to grasp it to clone it into different tags.

Your help is very appreciated!

hakre
  • 193,403
  • 52
  • 435
  • 836
RickyAYoder
  • 963
  • 1
  • 13
  • 29
  • Example using the search: http://stackoverflow.com/questions/8737846/php-bbcode-related-issue-how-to-get-values-between-two-tags – Prix May 05 '12 at 01:51
  • 2
    Please explain down votes. Downvoting this question tells nothing. (I beleive the approprate answer is< "Don't use Regex for parsing because *BBCOde is not a regular langues, *Regex is hard to maintain. instead use `X`" BUt not one has given it, they have just downvoted. --- Aso I suspect this is a duplicate post (No vote) – Frames Catherine White May 05 '12 at 05:28
  • I think the downvotes are as a result of the question not showing any prior research - we like prior effort here! That said, if you do a web search for _Markdown PHP_ you'll find a library that will do this for you, ready-made `:)`. – halfer May 05 '12 at 05:28
  • I've tried looking up PHP RegExp before, but all I see are lists of delimeters, and then a bunch of huge expressions that just seem a bit to hard for beginners like me. – RickyAYoder May 06 '12 at 03:53

2 Answers2

4

I have to imagine this exists out there for free somewhere, but here's how I'd do it.

// Patterns
$pat = array();
$pat[] = '/\[url\](.*?)\[\/url\]/';         // URL Type 1
$pat[] = '/\[url=(.*?)\](.*?)\[\/url\]/';   // URL Type 2
$pat[] = '/\[img\](.*?)\[\/img\]/';         // Image
// ... more search patterns here

// Replacements
$rep = array();
$rep[] = '<a href="$1">$1</a>';             // URL Type 1
$rep[] = '<a href="$1">$2</a>';             // URL Type 2
$rep[] = '<img src="$1" />';                // Image
// ... and the corresponding replacement patterns here


// Run tests
foreach($DIRTY as $dirty)
{
    $clean = preg_replace($pat, $rep, $dirty);

    printf("D: %s\n", $dirty);
    printf("C: %s\n", $clean);
    printf("\n");
}

Output:

D: Before [url]http://www.stackoverflow.com[/url] after
C: Before <a href="http://www.stackoverflow.com">http://www.stackoverflow.com</a> after

D: Before [url]http://www.stackoverflow.com[/url] [url]http://www.google.com[/url] after
C: Before <a href="http://www.stackoverflow.com">http://www.stackoverflow.com</a> <a href="http://www.google.com">http://www.google.com</a> after

D: Before [url=http://www.stackoverflow.com]StackOverflow[/url]
C: Before <a href="http://www.stackoverflow.com">StackOverflow</a>

D: Before [img]https://www.google.com/logos/2012/haring-12-hp.png[/img] after
C: Before <img src="https://www.google.com/logos/2012/haring-12-hp.png" /> after

For each $pat pattern element you add, you need to add a $rep element. The $DIRTY array is just a list of test cases and can be any length you think is sufficient.

The important parts here, and the parts that you would use are the $pat and $rep arrays and the preg_replace() function.

jedwards
  • 29,432
  • 3
  • 65
  • 92
  • Daedalus is more on the ball with what I had in mind--I've seen a friend of mine do it in about a few lines like that, though your method seems interesting to try. Thanks! – RickyAYoder May 05 '12 at 02:07
  • lol sigh, less lines != better -- especially in this case -- but good luck sir. – jedwards May 05 '12 at 02:10
  • Also, this is how you would convert both of the standard BBCode types of `[url]` tags as well as `[img]` tags. – jedwards May 05 '12 at 02:15
3

The user asked for something simple, so I gave him something simple.

$input = "[link=http://www.google.com]test[/link]";
$replacement = preg_replace('/\[link=(.*?)\](.*?)\[\/link\]/', '<a href="$1">$2</a>', $input);

Where /\[link=(.*?)\](.*?)\[\/link\]/ is the regex, <a href="$1">$2</a> is the format, $input is the input/data, and $replacement is the return.

rene
  • 41,474
  • 78
  • 114
  • 152
Daedalus
  • 7,586
  • 5
  • 36
  • 61
  • If you're using PHP4+, the `$n` notation is preferred over the `\\n` -- see [the doc](http://www.php.net/manual/en/function.preg-replace.php) – jedwards May 05 '12 at 02:13