Jump to content

replacing text inside quotes


dmikester1

Recommended Posts

I am trying to figure out how to use preg_replace to replace only the text within two double quotes.  If possible, I'd like to match text before the quotes as well, but only replace the text inside the quotes.  So here is my string:

$site = $fullName = "site name";

I need to replace the "site name" text, but leave the quotes intact.  Here is what I have right now.  It is replacing everything including the quotes.

$pattern = "/[\"][^\"]{2,}[\"]/";
$replace = preg_replace($pattern, $name, $nameLine);

 

Thanks

Mike

Link to comment
Share on other sites

Hi Mike,

 

Would this work for you? It replaces everything between the first pair of double quotes.

 

Input:

'blah "site name" blah'

 

Code:

$string='blah "site name" blah';
$pattern=',"\K[^"]+,';
$new='look at me!';
$s=preg_replace($pattern,$new,$string,1);
echo $s;

 

Output:

blah "look at me!" blah

 

 

Link to comment
Share on other sites

That works perfect!  Would you mind explaining the

Code: [select]

$pattern=',"\K[^"]+,';

line to me?

 

Hi Mike,

 

Glad it works!

First, I was going to mention that if you are using a version of PHP before 5.2.4, it wouldn't work and you would need something like:

$pattern=',(?<=")[^"]+,';

 

Okay, let's look at the pattern: ',"\K[^"]+,'

1. The commas at the front and back are the delimiters.

2. " matches a double quote.

3. \K says "drop what we have matched so far from what we will report as a match (so the engine keeps its position in the string, but it won't report the first quote in the match).

4. [^"]+ says match one or more characters (the + quantifier) that are not a double quote.

5. We don't need to tell the engine to match a double quote, because we know that what comes after "not a double quote" must be a double quote. Note that this is because you know how your string is built. This expression would also replace text that starts with a double quote, but has no closing double quote, such as 'blah "adg'

 

Let me know if you have more questions!

 

Wishing you a fun day

 

:)

 

Link to comment
Share on other sites

Hi Mike,

 

You can use pretty much whatever you want as a delimiter. (Not a backslash.)

For some odd reason, the forward slash is often used as an example. But it has lots of drawbacks.

Whenever you use a delimiter inside a string, you have to escape it. I don't know about you, but I don't find something like:

/http:\/\//

particularly nice to look at.

You'll find that a lot of regex heads have their favorite delimiters. You'll find a lot of #, %, ~, &, @.

I like the comma because it is unobtrusive. The expression itself really stands out. And when you use expressions in a number of places besides PHP, you tend to think of the expression without the delimiters. As an example, you wouldn't use delimiters in RegexBuddy.

 

 

Link to comment
Share on other sites

Addendum:

If you are creating your pattern programmatically, filling it with text you got from somewhere else, you may not know whether your string contains your delimiter (whether it's an ugly slash or a comma). In that case, preg_quote is your friend.

 

Here's an example. You are building a pattern based on an input string. You don't know what it's there, but you're worried it might have your delimiter (in this case, a comma).

 

$input='pass me the salt, she said';
$subject='LONG_TEXT pass me the salt, she said again LONG_TEXT';
$input=preg_quote($input,',');
$pattern=','.$input.'\s*\b\K\w+\b,';
preg_match($pattern,$subject,$match);
echo $match[0].'<br />';

 

Output: again

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.