1

I have the following code:

{bookielink href="bet-at-home"} body content{/bookielink}

and I want to get the following:

<a href="index.php?bookie=bet-at-home">body content</a>

This is my tries:

$buffer = preg_replace("/.*{bookielink[^>]*}|.*/si", "<a>", $buffer);
$buffer = preg_replace("/.*{\/bookielink}|.*/si", "</a>", $buffer);
3
  • possible duplicate of Best methods to parse HTML with PHP Commented Sep 10, 2011 at 11:01
  • 2
    @ajreal: not at all. Kicsi is trying to generate HTML from a custom format. Commented Sep 10, 2011 at 11:07
  • Sorry, and the down vote removed Commented Sep 10, 2011 at 11:10

1 Answer 1

1

The following:

$buffer = 'foo {bookielink href="bet-at-home"} body content{/bookielink} bar';
echo preg_replace(
    '#{bookielink\s+href="([^"]*)"\s*}([^{]+){/bookielink}#i', 
    '<a href="index.php?bookie=$1">$2</a>', 
    $buffer
);

will print:

foo <a href="index.php?bookie=bet-at-home"> body content</a> bar
Sign up to request clarification or add additional context in comments.

1 Comment

The regex seems fairly straightforward to me, but if you need some explanation, just say so, and I'll expand a bit.

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.