"Find A Unique String" (FAUS) Approach
Nearly every developer uses this technique to find the code responsible for some output that they want to modify. It's simple:
- Find a string in the UI that seems like it would be unlikely to occur elsewhere in the UI. Something like "Settings for the Image Toolkit".
- Search the entire project code base for the string using grep or an IDE. If you get few enough matches, you can probably figure out what code is responsible for the output.
- Adjust the string slightly and rebuild the UI to see if the change takes effect. If it does, BAM! You know where to get started. (note that you may need to rebuild your application or clear the application's caches to see the change take effect).
- You revert the change you just made so you don't forget about it.
Where to find a unique string
Finding a unique string becomes easier the more familiar you are with the application you're working with. At first, you may find yourself searching for strings and getting hundreds or thousands of results. Over time you'll get a sense of the usual suspects and try to find other strings that aren't as common.
While the text in the UI can provide a good source, the "id" and "class" properties of HTML is also a good place to look if you're building a browser-based app. Sometimes these are automatically generated, or the string is added to with prefixes or suffixes, but you can search parts of these properties to see if anything shows up.
If a search doesn't turn anything up
If you search the code base for the string and nothing comes up, here are some tips:
- Source is in a database: The string could be being pulled from a database, rather than code. If so, then you can do a database dump and search the database dump file. It will probably be harder to track down exactly how that string came to end up where it is, but it's still a useful clue.
- String has quotes: If there are any quotes in the string, then they may be escaped with a backslash, meaning that you're not searching for exactly the right string. To get around this, you can search for one side or the other of the string. For the string "you're in my searches!', you could search for "re in my searches".
- String has hidden whitespace: There my be some extra white space that you're not aware of. If you're searching HTML, you can look at the source code to see if there are extra spaces between words, or even extra lines. If you can just grab a single line to search for, you can avoid trying to add the white space to your search (adding white space to search is tricky). Again, you can use the approach of just searching for part of the string.
- Searching the wrong case: CSS or your backend code may be adjusting the case of the string, adding capitals or lowercase where it isn't in the source code. So, try searching with the "match case" box unchecked.
- String has been altered: The text could be being modified through various process. In particular, unique IDs may be prefixed or suffixed, so try searching for bits between underscores or dashes.
What to do once you've found the source
Typically, finding the source of the string you're looking for is the beginning of your search. The goal is to find the right place to adjust or add to what you see in the UI, and there might be several layers between this entry point and where you'll ultimately add your own code.
- Use a debugger: Using a debugger like Xdebug is really useful here, so you can see the full stack of method or function calls leading up this specific point in the code.
- Copy and paste: If you're dealing with something modular like a CMS where chunks of code and groups of files follow the same pattern for extending the application, you can probably start copying and pasting entire files or parts of code from various sources and replacing unique identifiers in function and variable names until you get something that can work in parallel with the original code you discovered.
Is there some other name for this approach?
If you know of a name for this technique (one that I didn't make up), please let me know so I can delete this page.