Find extra space / new line after a closing ?> (php tag)
grep ‘?> ‘ *.php ? Of course, it may not be a space and could be a linebreak or a tab, so you may want to try other characters.
Similar Question and Answer
for i in `find . -name "*.php"`; do (echo -n "$i: "; tail -c 3 $i) | grep -v "[?]>"; done
The idea is that you take just the last 3 characters with tail, then discard the files where those are ‘?’, ‘>’ and newline. If there’s a space or another newline, you won’t get the ‘?’ character..
sed -e :a -e '/^[ \n]*$/' -e '$ s/\([^ ]\)*?>[ ]*/\1?>/' file.php > new_file.php
to be executed for each file not completely tested..
remember to work on a temporary file and after the sed operation copy the new file on the original one..
Related questions and answers
If you need to use in in a sublime-settings file or something like that which doesn’t like forward slashes, you might have to add an extra slash for each of them like so.
This worked for me to find white spaces before php files
find -name '*.php' | xargs grep -Pz '\?>[\s]+$' -l
- -0777 will slurp he whole file (-0 will be ok too)
- -i — inplace editing, so the file will be replaces with the result
- -p print lines
- -e perl expression
s/\s*$//s — treat all lines as a single line and substitute any space at the end to nothing
clt60 59458
This is possible with regular grep
Will search for all files starting from the current directory and list all that have a ?> followed by white space at the end of the file.
- -P Interpret the pattern as a Perl-compatible regular expression (PCRE).
- -z Treats the input file as one long line — this is in part what makes it work
- [\s]+ matches at least one white space — including newlines
If you want to match PHP files only:
find -name '*.php' | xargs grep -Pz '\?>[\s]+$' -l
To search for white space at the beginning of the file before
find -name '*.php' | xargs grep -Pz '^[\s]+
The problem here is normal grep doesn't match multiple lines. So, I would install pcregrep and try the following command:
This will match all files in the folder and subfolders (the -r part) using PCRE multiline match (the -M part), and only list their filenames (the -l part).
As for the pattern, well that matches ?> followed by 1 or more whitespace or newline characters, followed by the end of the file \z . I found though, when I ran this on my folder, many of the PHP files do in fact end with a single newline. So you can update that regex to be '\?>[\s\n]+\n\z' to match files with whitespace over and above the single \n character terminator.
Lastly, you can always use od -c filename to print unambiguous representation of the file if you need to check its exact character sequence ending.
More Answer
- undefined javascript value after echoing a function's parameters in html onload tag php
- php errpr in admin section after shifting magento webstie to new server
- PHP members page carrying down to new line
- Find the Value segment after a field segment in PHP xml
- PHP end div after X amount of divs and start new one after X divs
- Php not parsing new line in variable obtained from GET request
- PHP Logging - won't add new line
- PHP Tidy removes the closing tag incorrectly
- Explode on new line in PHP only reads first line
- Why am I getting an extra space when I remove this line break?
- PHP Regex - Remove break line after li
- PHP Regex - Remove break line after li
- Putting new line feed in the text for an email php
- issues with abbr tag in php array. only shows until first space
- php str_replace new line lost
- php str_replace new line lost
- PHP DOM add new line between radio buttons
- Adding extra variables to new host php form
- Adding extra variables to new host php form
- how to find a particular tag in a string, get a value from it and then replace the string with PHP
- Remove closing tag XML root with PHP
- php auto array new line with exception with regex
- Converting \n in C# to a new line in PHP
- PHP Files not executing after using ' rm -rf * .' on /www/ and untaring new package
- Find a string in a text file and add text at the beginning to that line in php
- no new line in terminal after setting up cakephp
- php simplexml find text of html tag inside xml element
- php find numbers in a string after and before the word "euro"
- PHP : fread doesn't reads new line
- Find code line or file where php parameter is set
- Find code line or file where php parameter is set
- How add new line after four looping in smarty templates
More answer with same ag
- How to store Entry URL (Landing URL) and Referring site to a website using jquery/PHP
- Limiting file listing by filename length in PHP
- pass variables through imagemap
- Disable folder public access from htaccess
- Problems with executing FFMPEG through PHP
- foreach grabing the last element of a object array - why 0 retrieves it?
- Bypass cloudflare captcha in WordPress for Json API
- Using str_replace within an already-existing function?
- contact form works but method or closure "refresh" gives an error
- Magento - How to add Telephone and FAX in Account Create
- Search form errors
- PHP curl() Headers bad request
- PHP form post- submitted message
- Html dom parser problem in Domain
- Using cookies to keep someone logged in
- Yii / Gii : must extend from CModel
- How to use Gmail to send emails through Google API
- jFormer: Checkboxes and Address
- Return - Method Chaining
- Clarification on htaccess file use
- Scroll table upwards or downwards using up and down button
- PHP Opencart - Edit Orders
- PHP resize an image without saving a file
- removing (case insensitively) duplicates from array while trimming whitespace
- Image not showing in header when added in div
- redirect users using $_GET
- Securely allowing .zips and PDF in my upload script
- AS3 Read JSON from PHP
- Symfony2.3: unmapped fields missing in Form Event
- Symfony2 MimeTypeGuesser handling WMV files incorrectly
- PHP Date conversion date_create_from_format() not returning expected result
- AJAX drop-down-select site displaying information outside of table
- Exception handling in this PHP?
- task scheduling and PHPMailer class
- Add alt text to image in cute slider wordpress plugin
- add a php variable onto an url for cURL use
- how to add content in drupal using custom modules with forms Api
- Setting default Datepicker date using PHP session variable date
- Imgshot logout admin panel error
- CakePHP 3 Reset passwords
- how to run mysqli server on appserv 2.5.10?
- PHP mysqli_insert_id() Function without INSERT
- Table content keeps adding when refreshing PHP page
- How can i pass the id from the row clicked to a modal
- Typecasting on bitwise operators
- Sort input files after selection
- Email templates in magento
- jQuery - Ajax Error
- PHP Read and Write field values without a postback
- retrieving facebook userid ? Is it neccessary to have the signed request?
How to detect if a string has a new line break in it?
Obviously because the string doesn't have the "n" character in it. But how else can I check to see if there is a line break that is the result of the user pressing enter in a form field?
Answers
Your existing test doesn't work because you don't use double-quotes around your line break character ( 'n' ). Change it to:
Or, if you want cross-operating system compatibility:
Also note that strpos will return 0 and your statement will evaluate to FALSE if the first character is n , so strstr is a better choice. Alternatively you could change the strpos usage to:
if(strpos($string, "n") !== FALSE) < echo 'New line break found'; >else
Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):
$ pecl -d php_suffix=5.6 install $ pecl uninstall -r $ pecl -d php_suffix=7.0 install $ pecl uninstall -r $ pecl -d php_suffix=7.1 install $ pecl uninstall -r
The -d php_suffix= piece allows you to set config values at run time vs pre-setting them with pecl config-set . The uninstall -r bit does not actually uninstall it (from the docs):
[email protected]:~$ pecl help uninstall pecl uninstall [options] [channel/] . Uninstalls one or more PEAR packages. More than one package may be specified at once. Prefix with channel name to uninstall from a channel not in your default channel (pecl.php.net) Options: . -r, --register-only do not remove files, only register the packages as not installed .
The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).