Refactor lines with escaped quotation marks?
Back at the beginning of the century when I first learned HTML and PHP, I thought all strings in both languages had to be in double quotes. (Yeah, I know I was ignorant!) I now know I have options, but I have a pile of old code that looks like this:
echo "<td class=\"atype\" style=\"background-color:#".$row->BGColor."\">".$row->ActionType."</td>\n";
Is there a way to clean that up in one fell swoop? Ideally I'd like to be able to highlight a whole line (or even multiple lines when I have a bunch of similar echo statements together) and be given a choice to make it one of these:
echo "<td class='atype' style='background-color:#".$row->BGColor."'>".$row->ActionType."</td>\n";
echo '<td class="atype" style="background-color:#'.$row->BGColor.'">'.$row->ActionType.'</td>
';
The closest thing I've found is to put the cursor inside each quoted string, press Alt-Enter, and select "Replace Quotes" - that's way better than manual search/replace. But not only does it only do one string at a time (this example line has three), but more importantly, it only makes the change in one direction (the second option above). If I want the first option, i.e. the outer quotes to stay the same and just change `\"` to `'` (which is probably my preference in a case like this that includes a linefeed, because I find a literal linefeed inside a string confusing to read), the "Replace Quotes" intention doesn't appear to know how to do that. Ideas?
Yes, eventually I'll rewrite it in more of an MVC style instead of intermixing markup and business logic, but for now... baby steps.
请先登录再写评论。
Hello,
If you need just change `\"` to `'` then why don't try Edit > Find > Replace in Path?
It will quickly replace one to another in all files in selected directories or scope.
Simple Edit > Find > Replace also has option "Replace All".
I thought of that, but I'm afraid there might be cases where there is an apostrophe inside the string - maybe there aren't any, as it's mostly HTML attribute values, but I can't be sure. If you can think of a way to use regex or something to search for that possibility, that would help, but I can't figure out a way to do that.
It seems that PhpStorm has nothing to offer yet.
I can't figure out the correct regex either.
An issue that could help you if was fixed: https://youtrack.jetbrains.com/issue/WI-31219
Yeah, that would help if it worked. Anyway, thanks to both of you for brainstorming with me.