Preg_Replace Problem

Hi All,

I’m trying to create three variables for a preg_replace script - used ultimately for BBcode.

I can;t seem to get the syntax correct for a third variable. Here is my code:

'/\\[BBcode=(.+?)(.+?)](.+?)\\[\\/BBcode]/i', ... ;

This will split the initial thread in the first (), rather than create 2 seperate variables in the first . How do I separate the first two () so that they become two variables?

I’m following Kevin’s database book in the section on hyper-links in BBcode but am unable to extrapolate for this issue.

Thanks

Sure -

Here is the full preg_replace code:

$text = preg_replace ('/\\[BBcode=(.+?)(.+?)](.+?)\\[\\/BBcode]/i',
<a href="#" onMouseover="showtip(\\'$1\\')"; onMouseout="hidetip();"
onClick="$2">$3</a>, $text);

You can see that I am trying to capture 3 variables and place them within the html code. I can’t seem to capture the $2 variable.
Perhaps my BBcode needs to look something like:

[BBcode=(.+?){(.+?)}](.+?)\\[\\/BBcode]

Thanks-

I actually meant an example of what you’re trying to match

Does it look like:

[BBcode=a-1]b[/BBcode]

or… ?
Without an example I have no way of knowing how the regex should go since I have to idea what you’re trying to match exactly …

Thanks! It works.

Final code for those curious:

'/\\[BBcode=(.+?)\\((.+?)\\)\\](.+?)\\[\\/BBcode]/i', ...

\BBcode=(.+?)(\(.+?\))?\[\/BBcode]

Ah, I get it now, thanks for the info :slight_smile:

You can match a parenthesis in a regular expression by escaping them (putting a backslash in front of them):

This should work (not tested)

~\BBcode=(.+?)\(.+?)\)\\[/BBcode\]~

You may want to consider changing .+? for something more sensible like [a-zA-Z_-]+ :slight_smile:

Sorry 'bout that -

[BBcode=a-1]b[/BBcode]

This part of a form where the user inputs BBcode tags for english equivalents of french. An two variable example I can get working is:

[BBCode=english_word]french_word[/BBcode]

I’m not sure how to structure the three variable BBCode, perhaps:

[BBcode=english_word(onclick_variable)]french_word[/BBcode]

The issue is with the () in the first - I’m not sure how to delimit these as two separate variables.

Thanks

Can you give an example string that you’re trying to match?