Jump to content

[SOLVED] Replacing strings that may contain strings


Vinze

Recommended Posts

Hey, I use the following code to save all strings found in $code to $strings, and then replacing all strings with _|_|_|_:
[code=php:0]$pattern = '[(\'(.*?)\')|("(.*?)")]';
preg_match_all($pattern, $code, $strings);
$parsed = preg_replace($pattern, '_|_|_|_', $code);[/code]
This all works fine, until I get to a string like [code=php:0]echo "bla'bla'bla";[/code], which would become [code=php:0]echo "bla_|_|_|_bla";[/code]. How can I make sure this will become [code=php:0]echo _|_|_|_;[/code]?

Thanks in advance.

PS. Of course I also want [code=php:0]echo 'bla"bla"bla';[/code] to be replaced by [code=php:0]echo "bla_|_|_|_bla";[/code].
From the looks of it:

1. It's OK for singles to be inside of doubles. The doubles take higher precedence and engulf the singles.
2. It's [u]not[/u] OK for doubles to be inside of singles, or, in other words, singles cannot engulf doubles.

If this is correct, change your singles pattern to disallow doubles inside, going from[tt] \'(.*?)\' [/tt]to[tt] \'([^"]*?)\'[/tt].
[quote author=effigy link=topic=121169.msg497727#msg497727 date=1168033266]
From the looks of it:

1. It's OK for singles to be inside of doubles. The doubles take higher precedence and engulf the singles.
2. It's [u]not[/u] OK for doubles to be inside of singles, or, in other words, singles cannot engulf doubles.

If this is correct, change your singles pattern to disallow doubles inside, going from[tt] \'(.*?)\' [/tt]to[tt] \'([^"]*?)\'[/tt].
[/quote]

You're awesome :D

The pattern I used was [code=php:0]$pattern = '[(\'([^\']*?)\')|("(?!\')([^"]*?)")]';[/code]
A bit different, I don't know why it works now but I'll read up on that, thanks!
Hmmm.... You should only need[tt] (.*?) [/tt]within your single quotes. The pattern won't gobble up any more than 2 single quotes since your quantifier is non-greedy (*?), thus giving you: single_quote, anything_but_a_single_quote, single_quote.

I also do not see the need for[tt] (?!\')[/tt], since it would only reject a single quote directly following the first double--perhaps this is my ignorance of your data.
[quote author=effigy link=topic=121169.msg498315#msg498315 date=1168115387]
Hmmm.... You should only need[tt] (.*?) [/tt]within your single quotes. The pattern won't gobble up any more than 2 single quotes since your quantifier is non-greedy (*?), thus giving you: single_quote, anything_but_a_single_quote, single_quote.

I also do not see the need for[tt] (?!\')[/tt], since it would only reject a single quote directly following the first double--perhaps this is my ignorance of your data.
[/quote]
I don't get it either, I just know it works and that's enough for me ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.