Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-16-2008, 03:59 PM   PM User | #1
mlse
Regular Coder

 
mlse's Avatar
 
Join Date: Mar 2005
Posts: 624
Thanks: 20
Thanked 19 Times in 18 Posts
mlse is on a distinguished road
preg_replace problem

Hi all,

I want to replace strings matching the pattern {#<whatever>} (where whatever may vary) with the corresponding replacement, but only in unquoted strings. I know how to do this in all strings, but not in only unquoted ones.

for example:

PHP Code:
$string "The \"quick {#colour}\" {#animal} jumps over the '{#adjective} dog'"
I know how to get preg_replace to replace {#colour}, {#animal} and {#adjective} with replacement strings, BUT what (generic) pattern should I use to make preg_replace ignore {#colour} and {#adjective} and only replace {#animal}?

I.e. I want to ignore matching patterns in strings-within-the-string.

Any help much appreciated

Last edited by mlse; 02-16-2008 at 04:25 PM..
mlse is offline   Reply With Quote
Old 02-16-2008, 08:23 PM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
what you want to do cannot be done using a regex...you need to write a simple parser to handle quoted strings within strings
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Old 02-18-2008, 09:40 AM   PM User | #3
mlse
Regular Coder

 
mlse's Avatar
 
Join Date: Mar 2005
Posts: 624
Thanks: 20
Thanked 19 Times in 18 Posts
mlse is on a distinguished road
I thought as much! Thanks anyway
mlse is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:46 AM.


Advertisement
Log in to turn off these ads.