I have a need to convert source data that I can't control into normalization form C. I am currently doing it, but by calling an external program (uconv). This is what my code snippet looks like:
$malayalam_books = preg_split("/\n/", shell_exec("uconv -f utf8 -t utf8 -x nfc book-names.txt"));
It works well, but obviously making calls to the system is not recommended. I know that PHP supports the ICU libraries, but it's so convoluted how to do a simple thing like this...
I've since discovered that the normalizer_normalize PECL function can handle this natively in PHP 5 >= 5.3.0.
Related
I am working on a command-line-script witch uses fread (for yes/no,...) and fgets (for manually validating values,...).
The problem is if I enter more than ... chars (\n is a char, too), this chars are already inputed in the next fgets/fread-function.
I tried fflush(STDIN), but it didn't work. I also tried stream_get_line(STDIN,1024); which was recommended by someone in a simmelar question, but then I have to input nearly unendless much chars until the programm continues (I quit).
How to get PHP to delete keyboard-buffer before using fgets/fread?
If your PHP is compiled with the Readline module (it's quite common) you may just use readline()
http://php.net/manual/en/function.readline.php
$input = readline( 'Prompt: ' );
readline() simplifies the process of reading input from the terminal.
I'm looking for an free php library that can generate code diff HTML. Basically just like GitHub's code diffs pages.
I've been searching all around and can't find anything. Does anyone know of anything out there that does what I'm looking for?
It looks like I found what I'm looking for after doing more Google searches with different wording.
php-diff seems to do exactly what I want. Just a php function that accepts two strings and generates all the HTML do display the diff in a web page.
To add my two cents here...
Unfortunately, there are no really good diff libraries for displaying/generating diffs in PHP. That said, I recently did find a circuitous way to do this using PHP. The solution involved:
A pure JavaScript approach for rendering the Diff
Shelling out to git with PHP to generate the Diff to render
First, there is an excellent JavaScript library for rendering GitHub-style diffs called diff2html. This renders diffs very cleanly and with modern styling. However diff2html requires a true git diff to render as it is intended to literally render git diffs--just like GitHub.
If we let diff2html handle the rendering of the diff, then all we have left to do is create the git diff to have it render.
To do that in PHP, you can shell out to the local git binary running on the server. You can use git to calculate a diff on two arbitrary files using the --no-index option. You can also specify how many lines before/after the found diffs to return with the -U option.
On the server it would look something like this:
// File names to save data to diff in
$leftFile = '/tmp/fileA.txt';
$rightFile = '/tmp/fileB.txt';
file_put_contents($leftFile, $leftData);
file_put_contents($rightFile, $rightData);
// Generate git diff and save shell output
$diff = shell_exec("git diff -U1000 --no-index $leftFile $rightFile");
// Strip off first line of output
$diff = substr($diff, strpos($diff, "\n"));
// Delete the files we just created
unlink($leftFile);
unlink($rightFile);
Then you need to get $diff back to the front-end. You should review the docs for diff2html but the end result will look something like this in JavaScript (assuming you pass $diff as diffString):
function renderDiff(el, diffString) {
var diff2htmlUi = new Diff2HtmlUI({diff: diffString});
diff2htmlUi.draw(el);
}
I think what you're looking for is xdiff.
xdiff extension enables you to create and apply patch files containing differences between different revisions of files.
This extension supports two modes of operation - on strings and on files, as well as two different patch formats - unified and binary. Unified patches are excellent for text files as they are human-readable and easy to review. For binary files like archives or images, binary patches will be adequate choice as they are binary safe and handle non-printable characters well.
I'm currently successfully reading out several properties on our switches over SNMP with php. Now i'm looking at making the resulting output of snmpget and snmpwalk actually usefull for the consumers of our API's.
Problem is that the responses look like this: INTEGER: up(1) and INTEGER: 10103 ...
Is there any convention/standard on how to parse this response format or is the response vendor specific for each device we are trying to read?
Is there by any chance already a PHP library, function or extension that can cast these responses in php native variables or at least something usefull that we can work with?
UPDATE:
I've found out a few new things namely that there are indeed several libraries in php that can parse binary ASN.1 strings which basically are BER encoded strings if i'm right. Problem is that i can't seem to find a way to get the binary data from the devices with php ...
You can simply use this function at the beginning of your script :
snmp_set_quick_print(TRUE);
It will returns only the value you are searching for, without the leading "INTEGER" or so ;)
Hope this helps !
I'm not sure about your particular PHP methods, but the difference between your two INTEGER examples is likely to be whether your system has an SNMP MIB corresponding to the OID (e.g. to determine that 1 means "up").
If you only want the integers, you should be able to pass a parameter to your get or walk command. For example, net-snmp's snmpget or snmpwalk commands will take -Oe to remove symbolic labels. From the manpage:
$ snmpget -c public -v 1 localhost ipForwarding.0
IP-MIB::ipForwarding.0 = INTEGER: forwarding(1)
$ snmpget -c public -v 1 -Oe localhost ipForwarding.0
IP-MIB::ipForwarding.0 = INTEGER: 1
If you are parsing net-snmp output, I recommend reading the snmpcmd man page as it has a lot of output options that will interest you especially the display of other types such as timeticks and strings.
If you do want to retrieve SNMP in PHP you could look at how Cacti does it.
I am looking for a command-line tool that removes all comments from an input
file and returns the stripped output. It'd be nice it supports popular
programming languages like c, c++, python, php, javascript, html, css, etc. It
has to be syntax-aware as opposed to regexp-based, since the latter will catch
the pattern in source code strings as well. Is there any such tool?
I am fully aware that comments are useful information and often leaving them
as they are is a good idea. It's just that my focus is on different use cases.
cloc, a free Perl script, can do this.
Remove Comments from Source Code
How can you tell if cloc correctly identifies comments? One way to convince yourself cloc is doing the right thing is to use its --strip-comments option to remove comments and blank lines from files, then compare the stripped-down files to originals.
It supports a lot of languages.
What you want can be done with emacs scripting.
I wrote this script for you which does exactly what you want and can be easily extended to any language.
Filename: kill-comments
#!/usr/bin/python
import subprocess
import sys
import os
target_file = sys.argv[1]
command = "emacs -batch -l ~/.emacs-batch " + \
target_file + \
" --eval '(kill-comment (count-lines (point-min) (point-max)))'" + \
" -f save-buffer"
#to load a custom .emacs script (for more syntax support),
#use -l <file> in the above command
#print command
fnull = open(os.devnull, 'w')
subprocess.call(command, shell = True, stdout = fnull, stderr = fnull)
fnull.close()
to use it just call:
kill-comments <file-name>
To add any language to it edit ~/.emacs-batch and add that language's major mode.
You can find syntax aware modes for basically everything you could want at http://www.emacswiki.org.
As an example, here is my ~/.emacs-batch file. It extends the above script to remove comments from javascript files. (I have javascript.el in my ~/.el directory)
(setq load-path (append (list (concat (getenv "HOME") "/.el")) load-path))
(load "javascript")
(setq auto-mode-alist (cons '("\\.js$" . javascript-mode) auto-mode-alist))
With the javascript addition this will remove comments from all the filetypes you mentioned as well as many more.
Good Luck and happy coding!
Paul Dixon's response to this question on stripping comments from a script might be worth looking at.
I don't know of such a tool - which isn't the same as saying there isn't one.
I once started to design one, but it quickly gets insane - not helped by the comment rules in C and C++.
/\
* Comment? *\
/
(Answer: yes!)
"/\
* Comment? *\
/"
(Answer: no!)
To do the job reasonably, you have to be aware of:
Language comment conventions
Language quoted string conventions (Python and Perl are enough to drive you insane here)
Escape conventions (Shell gets you here - along with the quotes)
These combine to make the job tolerably close to impossible.
I ended up with a program, scc, to strip C and C++ comments. Its torture test includes worse examples than the comments shown above - and it does a decent job. But extending that to do shell or Perl or Python or (take your pick) was sufficiently non-trivial that I did not do it.
No such tool exists yet.
You might coax GNU Source-highlight into doing this.
I want to use Prolog with PHP. Is it possible?
There are always the exec-familiy functions to execute/spawn another process.
As suggested above, you can execute a Prolog interpreter or binary. However, most Prolog implementations also export a C API that can be used to call the Prolog interpreter.
You could create a small PHP module to start an interpreter and execute queries. For instance, the SICStus documentation describes using Prolog from C in detail:
6 Mixing C/C++ and Prolog
Most Prologs allow for prolog code to be compiled into a binary. You could try calling this binary from within PHP using some kind of process call as already mentioned.
I wrote a translator that can convert some simple PHP programs into Prolog and vice-versa.
This is a possible input program:
function is_greater_than($a,$b){
return $a > $b;
}
function is_an_animal($a){
return in_array($a,["dog","cat"]);
}
...and this is the output program:
is_greater_than(A,B) :-
A>B.
is_an_animal(A) :-
member(A,["dog","cat"]).
This translator is only a proof-of-concept, but it may still make it easier to convert some simple PHP programs into Prolog.