- Saved searches
- Use saved searches to filter your results more quickly
- License
- quasilyte/phpgrep
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- Php grep all files ending in
- Grep check PHP files starting or ending with EOL
- Regex to remove last PHP tag in files
- How to list all files with white spaces the after php tag
- Finding a string at the end of a line using shell_exec grep UNIX
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Syntax-aware grep for PHP code.
License
quasilyte/phpgrep
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Syntax-aware grep for PHP code.
This repository is used for the library and command-line tool development. A good source for additional utilities and ready-to-run recipes is phpgrep-contrib repository.
phpgrep is both a library and a command-line tool.
Library can be used to perform syntax-aware PHP code matching inside Go programs while binary utility can be used from your favorite text editor or terminal emulator.
It’s very close to the structural search and replace in PhpStorm, but better suited for standalone usage.
If you’re using VS Code, you might be interested in vscode-phpgrep extension.
Download a phpgrep binary from the latest release, put it somewhere under your $PATH .
Run a -help command to verify that everything is okay.
$ phpgrep -help Usage: phpgrep [flags. ] targets pattern [filters. ] Where: flags are command-line arguments that are listed in -help (see below) targets is a comma-separated list of file or directory names to search in pattern is a string that describes what is being matched filters are optional arguments bound to the pattern Examples: # Find f calls with a single varible argument. phpgrep file.php 'f($)' # Like the previous example, but searches inside entire # directory recursively and variable names are restricted # to $id, $uid and $gid. # Also uses -v flag that makes phpgrep output more info. phpgrep -v ~/code/php 'f($)' 'x=id,uid,gid' # Run phpgrep on 2 folders (recursively). phpgrep dir1,dir2 '"some string"' # Print only matches, without locations. phpgrep -format '>' file.php 'pattern' # Print only assignments right-hand side. phpgrep -format '>' file.php '$_ = $rhs' # Ignore vendored source code inside project. phpgrep --exclude '/vendor/' project/ 'pattern' Custom output formatting is possible via the -format flag template. > match containing file name > line number where the match started > a source code line that contains the match > an entire match string > $x submatch string (can be any submatch name) The output colors can be configured with "--color-" flags. Use --no-color to disable the output coloring. Exit status: 0 if something is matched 1 if nothing is matched 2 if error occurred # . rest of output
Create a test file hello.php :
function f(. $xs) <> f(10); f(20); f(30); // aha! f($x); f();
Run phpgrep over that file:
$ phpgrep hello.php 'f($)' 'x!=20' hello.php:3: f(10); hello.php:5: f(30); // aha! found 2 matches
We found all f calls with a single argument x that is int literal not equal to 20.
Next thing to learn is $ matcher.
Suppose you need to match all foo function calls that have null argument.
foo is variadic, so it’s unknown where that argument can be located.
This pattern will match null arguments at any position: foo($, null, $) .
Read pattern language docs to learn more about how to write search patterns.
Read the user manual to learn more about phpgrep command line arguments and to get some insights on how to use it.
This section contains ready-to-use phpgrep patterns.
srcdir is a target source directory (can also be a single filename).
# Find arrays with at least 1 duplicated key. $ phpgrep srcdir '[$, $k => $_, $, $k => $_, $]' # Find where `$x ?: $y` can be applied. $ phpgrep srcdir '$x ? $x : $y' # Use `$x ?: $y` instead # Find where `$x ?? $y` can be applied. $ phpgrep srcdir 'isset($x) ? $x : $y' # Find in_array calls that can be replaced with $x == $y. $ phpgrep srcdir 'in_array($x, [$y])' # Find potential operator precedence issues. $ phpgrep srcdir '$x & $mask == $y' # Should be ($x & $mask) == $y $ phpgrep srcdir '$x & $mask != $y' # Should be ($x & $mask) != $y # Find calls where func args are misplaced. $ phpgrep srcdir 'stripos($, $_)' $ phpgrep srcdir 'explode($_, $, $)' # Find new calls without parentheses. $ phpgrep srcdir 'new $t' # Find all if statements with a body without <>. $ phpgrep srcdir 'if ($cond) $x' 'x!~^\' # Or without regexp. $ phpgrep srcdir 'if ($code) $' # Find all error-supress operator usages. $ phpgrep srcdir '@$_' # Find all == (non-strict) comparisons with null. $ phpgrep srcdir '$_ == null'
# Find all function calls that have at least one var-argument that has _id suffix. $ phpgrep srcdir '$f($, $, $)' 'x~.*_id$' # Find foo calls where the second argument is integer literal. $ phpgrep srcdir 'foo($_, $)'
You’ll need Go tools to install phpgrep from sources.
To install phpgrep binary under your $(go env GOPATH)/bin :
go get -v github.com/quasilyte/phpgrep/cmd/phpgrep
If $GOPATH/bin is under your system $PATH , phpgrep command should be available after that.
Php grep all files ending in
This regex can also be used with grep. Question: I’m updating some old code, and noticed there’s a lot of files that still have the old-style PHP file ending, where the is the last chars in the file. *? — match everything between tags Question: Is it possible with linux commands to List all files with white spaces the after php tag at the end of file?
Grep check PHP files starting or ending with EOL
So, I just spend an hour looking through A LOT of php files for en EOL after a closing ?>. I tried regex matching it to make it easier, but with a big project \?>(\n)$ still ended up getting me 450+ results. That, and the problem turned out to be an EOL BEFORE the opening
So, I guess there HAS to be a better way. I was thinking GREP looking for files starting or ending with an EOL.
So what is the best way to solve these issues?
I ended up using a regex in Eclipse and searching all . PHP file s.
I ran /(^<\?php|\?>\s$)/g and found out I had two EOL before an opening
This regex can also be used with grep.
Php — How can i find a function which is included in multiple files, Use ‘man grep’ and ‘man find’ from your *nix command line to learn more. If you are on Windows, I believe the find command can do similar things
Regex to remove last PHP tag in files
I’m updating some old code, and noticed there’s a lot of files that still have the old- style PHP file ending, where the ?> is the last chars in the file.
NOTE: this is in keeping with many coding standards for PHP eg.: http://www.php-fig.org/psr/psr-2/
Is there a way to quickly remove these directory wide (via preg_replace , maybe grep / sed , or whatever) but not remove legitimate closing tags in in-line PHP blocks?
I have no problem finding / replacing these— I am not sure how to ensure that it’s the last characters in the file though.
Firstly, as with all bulk search and replace tasks, be sure you have backed up the files so you have something to fall back on in case this doesn’t work.
Then, on the command line, try:
$ sed -i '$s/\([[:blank:]]\)*?>\([[:blank:]]\)*$//g' "$(grep -rl '^\([[:blank:]]\)*?>\([[:blank:]]\)*$')"
- uses command substitution $() containing a grep command
- grep -r to recursively find files. If your directory has mixed in non- PHP files and takes too long, we can work in a find , however for now try this
- grep -l to just list, don’t show the match, thus pass file names to sed command
- [:blank:] is a POSIX character class to match space or tabs.
- so the grep matches for a line beginning with space or tab for zero+ characters, the ?> , followed by space or tab zero+ characters, then end of line.
- this is to deal with edge cases where the code does not end as expected with just ?> but for whatever odd reason you happen to have extra white spaces before and after the ?>
- this grep alone will also include unwanted results where you happen to have ?> on its own line in the middle of the PHP script, so to focus only on end-of-file, last line, we have the sed
- -i replaces in place. Could also have used -i.bak to automatically have sed create a *.bak file backup, but I prefer not to clutter the web server with *.bak files, and if you followed my recommendation to backup prior to this, you already have a backup and won’t need this
- the sed command starting with $ specifies the address is the last line
- then the action to take part on that address is a replacement similar to what grep was looking for
- the sed acts via a replacement, so will still leave a blank line, which at least ensures conformity with PSR-2 All PHP files MUST end with a single blank line. requirement
- if you aren’t getting any fixes at all, it could be DOS vs Linux line ending issues preventing grep from working, in which you may need to use dos2unix on the PHP files and then re-try this command
The result is the successful elimination of last line ?> , even if there were extra «unclean» spaces before or after ?> .
I faced very similar issue recently. In order to fix this I decided to replace content of .php files in Notepad++.
1) Firstly backup all your files in working directory 2) Secondly use following regex in notepad++ to replace all .php files:
It matches all files starting with . Additionally it removes white spaces before and after tags. If you do not want this behavior to appear, remove the (\s+)? part from regex.
\A — means beginning of file \s — matching white spaces \Z — means end of file .*? — match everything between tags
How to list all files with white spaces the after php tag
Is it possible with linux commands to List all files with white spaces the after php tag at the end of file?
Answer using pcregrep which will match even if your whitespace spans onto new lines:
Untested though because I don’t have pcregrep installed on this computer. Please correct me if you know it to be incorrect.
This should match all files that end with ?> followed by only whitespace. If it’s followed by any other character, then it won’t match (e.g. if it’s not an all-PHP file and needs ?> )
Delete all trailing blank lines at end of file.
Check for files with whitespace after the closing tag using grep -El ‘.*\?>\s+’ *.php :
$ cat -E nospace.php $ $ $ cat -E withspace.php $ $ $ grep -El '.*\?>\s+' *.php withspace.php
Originally posted as a comment:
A better plan: remove all closing tags.
Regex match all not PDF files [duplicate], It simply uses a negative lookahead to assert that the filename doesn’t end in .pdf and then .* to match everything in the filename.
Finding a string at the end of a line using shell_exec grep UNIX
I’m trying to find a string at the end of a line in files using
This returns nothing but if i remove the $ it will return all matches (even if not at the end of the line.)
Please let me know what I’m doing wrong.
for searching strings, chars and file extensions in lines
Grep check PHP files starting or ending with EOL, Hint, partial solution: you can use grep -rn -B1 «