Jump to content

Replacing content in double quotes but not if the doubles quotes are in quotes


zdp

Recommended Posts

Ok, so I wasn't able to find an issue similar to what I've been struggling with, so I apologize if it has been answered or is really easy :-P

 

So here's my problem: I am importing a file using file_get_contents and want to replace any text that is inside of a double quote unless the double quote is in a single quote itself.

 

The regex I have now will do the former, but not the latter. The regex I am using is '/"(.+?)"/s', and I am using preg_replace.

 

Here are 2 examples and what their expected results should be:

##div('$$title or "$$title"');
// current results: '$$title or "$view->title"'
// expected results: '$$title or "$$title"'

##div("$$title or '$$title'");
// current results: "$view->title or '$view->title'"
// expected results: "$view->title or '$view->title'"

 

If you notice the first example, the single quoted string should be ignored, whereas right now it is not. That's what I need to fix.

 

Let me know if this makes sense. Thanks!

Link to comment
Share on other sites

This is only a basis as unpaired single quotes (apostrophes) may throw off the detection.

 

<pre>
<?php
### $'s escaped for sake of example.
$data = <<<DATA
##div('\$\$title or "\$\$title"');
// current results: '\$\$title or "\$view->title"'
// expected results: '\$\$title or "\$\$title"'

##div("\$\$title or '\$\$title'");
// current results: "\$view->title or '\$view->title'"
// expected results: "\$view->title or '\$view->title'"
DATA;

echo '<b>Original:</b><hr>';
echo $data;

echo '<br><br><b>Split on Double Quote Pairs:</b><hr>';
$pieces = preg_split('/(".*?")/s', $data, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($pieces);

echo '<br><br><b>Double Quote Replacement:</b><hr>';
$singles = 0;
foreach ($pieces as &$piece) {
	$singles += substr_count($piece, "'");
	### No replacement should be made if:
	### 1. The line does not start with a double quote; and
	### 2. If the number of single quotes are not even (closed).
	if (!substr($piece, 0, 1) == '"' || $singles % 2) {
		continue;
	}
	$piece = preg_replace('/"(.*?)"/s', '"====="', $piece);
}
print_r($pieces);

echo '<br><br><b>Reassembled:</b><hr>';
echo join('', $pieces);

?>
</pre>

Link to comment
Share on other sites

  • 2 weeks later...

Something like... untested, but it should work.

 

<?php

$text = '##div("$$title1 or \'$$title2\'");
##div(\'$$title3 or "$$title4"\');
';

echo preg_replace_callback ( '/(".*("(?!\')))/Usi', create_function('$match', 'return str_replace ( "\$\$", "\$view->", $match[0] );'), $text );


?>

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.