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].
Link to comment
Share on other sites

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].
Link to comment
Share on other sites

[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!
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[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 ;)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.