Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...It's extraordinarily refreshing to see truly expert advice without having to wade through hipper than thou attitude..."

Geography

Where in the world do Tek-Tips members come from?

preg_replace with backreference AND curly bracesHelpful Member! 

OsakaWebbie (Programmer)
11 Jun 12 3:18
Trying to use PHP to construct TeX code, and TeX loves curly braces. But in PHP, curly braces are interpreted as part of parsing variables. I have strings like "Dm7 G" (yes, they are guitar chords), and want them to become like "Dm\Sup{7} G".

If I do this:

CODE --> PHP

preg_replace("/([0-9]+)/","\\Sup{$1}",$chord)
I get a syntax error.

But if I do this:

CODE --> PHP

preg_replace("/([0-9]+)/","\\Sup\{$1\}",$chord)
the backslashes actually end up in the resulting string, which is not desired.

Suggestions?
Helpful Member!  feherke (Programmer)
11 Jun 12 4:24
Hi

Quote (OsakaWebbie)

But in PHP, curly braces are interpreted as part of parsing variables.
Correct. So you have to stop the variable parsing :

CODE --> PHP

preg_replace("/([0-9]+)/","\\Sup{\$1}",$chord);

May be caused by my affinity for shell scripting, but I generally prefer to use the most restrictive quoting possible in each case. In this case I would use single quotes ( ' ) :

CODE --> PHP

preg_replace('/([0-9]+)/','\\Sup{$1}',$chord);

Feherke.
http://feherke.github.com/

OsakaWebbie (Programmer)
11 Jun 12 11:13
Thanks! I must be losing my mind - I thought I tried that second version (among various things I tried in desperation) and got an error. Anyway, naturally you know what you're doing, and both solutions work.

Quote (feherke)

So you have to stop the variable parsing...
I'm a little confused - when I stop the parsing (either by escaping the $ or using single quotes), why don't I end up with a literal "Dm\Sup{$1} G"?
feherke (Programmer)
11 Jun 12 11:25
Hi

Quote (OsakaWebbie)

when I stop the parsing (...) why don't I end up with a literal "Dm\Sup{$1} G"?
In this case $1 is not a variable, it is a placeholder. The preg_replace() function replaces such placeholders after her finds a substring matching the regular expression and extracted the captured groups.

Variable parsing had to be stopped to avoid the PHP interpreter trying to interpret $1 as variable before the preg_replace() function receives its string parameters.

Feherke.
http://feherke.github.com/

OsakaWebbie (Programmer)
11 Jun 12 19:53
Okay, thanks for the clarification. smile

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close