By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,984 Members | 2,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,984 IT Pros & Developers. It's quick & easy.

preg_replace backreferencing problem

P: n/a
I've been puzzling over something for ages and now give up:

Does anyone see any problem with any of the following:

$line = preg_replace('/\[#([A-Fa-z0-9]{1,6})\]/i', "<font color='#
${1}'>", $line);

$line = preg_replace('/\[(#(([A-Fa-f0-9]){3}(([A-Fa-f0-9]){3})?))\]/
i', "<font color='#${1}'>", $line);

I tried both ; both don't work.

This snippet should convert something like [#FF0000] (hex colours)
into an HTML font tag. I believe the reg.exp IS matching (since I do
get a <fonttag), but the backreferencing is not working since the
font tag would contain an empty hex code.

I've tried using \\1 for the backreferencing, or even \\0 just to test
- but even that doesn't work.

Is it possible that backreferencing can just be turned off?!?

- Terence

May 24 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On May 25, 6:24 am, Terence <terence.par...@gmail.comwrote:
I've been puzzling over something for ages and now give up:

Does anyone see any problem with any of the following:

$line = preg_replace('/\[#([A-Fa-z0-9]{1,6})\]/i', "<font color='#
${1}'>", $line);
try:
$line = preg_replace('/\[#([A-Fa-z0-9]{1,6})\]/i', "<font color='#
$1'>", $line);

May 25 '07 #2

P: n/a
On 25.05.2007 01:24 Terence wrote:
I've been puzzling over something for ages and now give up:

Does anyone see any problem with any of the following:

$line = preg_replace('/\[#([A-Fa-z0-9]{1,6})\]/i', "<font color='#
${1}'>", $line);

$line = preg_replace('/\[(#(([A-Fa-f0-9]){3}(([A-Fa-f0-9]){3})?))\]/
i', "<font color='#${1}'>", $line);

I tried both ; both don't work.

This snippet should convert something like [#FF0000] (hex colours)
into an HTML font tag. I believe the reg.exp IS matching (since I do
get a <fonttag), but the backreferencing is not working since the
font tag would contain an empty hex code.

I've tried using \\1 for the backreferencing, or even \\0 just to test
- but even that doesn't work.

Is it possible that backreferencing can just be turned off?!?

- Terence
$line = preg_replace('/\[(#[A-F0-9]+)\]/i', "<font color='$1'>", $line);

the correct syntax is $1 not ${1} or similar.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.