How can I grep a pattern that can include spaces, commas etc using exec or system in PHP, and return one string separated by a comma each result. If needed, I can make use of awk or sed to separated by comma.
So for instance, if the pattern to search is: hello worl
then return:
$result = 'hello world, hello worlds, hello worldy';
Any ideas will be appreciated. Thanks
Check out escapeshellarg() and escapeshellcmd().
Edit:
Is this in just one file? If you're not searching across the system then just use the built-in functions, they'll be plenty fast.
Related
If I run the following in PHP
<?=hash('sha256', "\v");?>
I get this hash:
e7cf46a078fed4fafd0b5e3aff144802b853f8ae459a4f0c14add3314b7cc3a6
However if I put \v into any of the online tools like http://www.xorbin.com/tools/sha256-hash-calculator, I get the following hash:
1b5cec8c46451b5375ea7e61f310fe831ad17f62098beb7a5bfce304821e3f78
I realise that PHP is obviously hashing the escaped value, rather than the actual string,
\v vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
(Reference: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double)
but what I'm trying to figure out is how to get the same result as the PHP function using the command line or one of the online tools - I've tried VT, vt, 0x0B and 11 - but none of them give me the same result.
Thanks for your help!
UPDATE: As #Alex-Info.net pointed out, changing the double quotes to single quotes will treat the \v as a string, but unfortunately I can't change the PHP in this instance.
As I said, I'd favor chaing your PHP code to match the online/commandline apps. Single quotes will not escape, and gets the results you mentionned.
<?= hash('sha256', '\v'); ?>
EDIT: Since you can't change the PHP code, here's how to tweak the command line so that it escapes correctly like PHP does:
echo -e -n "\v" | openssl dgst -sha256 , -e enables escaping.
I am quite familiar with sed on bash shell, so I do
cat $myfile | egrep $pattern | sed -e 's/$pattern/$replace/g'
a lot. Quite a few times, I find that I need to do similar kind of "string parsing" inside PHP.
So my question is simple, what is the equivalent of sed -e 's/$pattern/$replace/g' in PHP ?
I know preg_match , preg_replace , but I haven't used them / not at all familiar with them. I would really appreciate sample code in PHP. (converting say a $var = '_myString' to $newVar = 'getMyString' )
The preg_ functions uses the same regexp library that Perl does, so you should be at home. There's documentation of the syntax here.
For example:
sed -e 's/$pattern/$replace/g'
Would be something like:
$output = preg_replace("/".$pattern."/", $replace, $input);
The most common is to use / as delimiter, but you can use other characters, which can be useful if the pattern contains lots of slashes, as is the case with urls and xml tags. You may also find preg_quote useful.
I'm creating a 'bad words' filter as str_ireplace function and I have a list of about 500 bad words.. all in a long vertical list. Any idea how I could quickly and easily create a horizontal, comma-delimited formatted list without manually typing a comma after every word and backspacing?
And yes.. I could probably do this in 20 minutes, but I've had this problem before so I'm asking for all the future times I run into this too.
I'd just use find and replace. If whatever editor you're using for your coding can't cope with finding carriage returns try Word, Notepad++, etc.
In php it would be something like:
str_replace(array("\r", "\r\n", "\n"), ", ", $string);
Or
$file = file("list.txt");
print_r($file);
Or, if you want to use bash for that, this would be the thing:
sed -e :a -e '$!N;s/\n/, /;ta' list.txt
file_put_contents('filename', implode(',', file('filename', FILE_IGNORE_NEW_LINES)));
This code rewrite your file exactly as you want
I have a long list of regular expressions in an ignore.txt, and another long list in an include.txt file. What would be the quickest way to apply these using PHP against data contained in a sample.html file such that any matches found in include are captured, but then anything matching in ignore.txt is excluded?
If your include.txt and ignore.txt files are setup so that they're only regular expressions, and there's one expression per line, you can use PHP's file() function. That will load the files into an array where each individual line is an element in the array. You can use file_get_contents() to load the sample.html file in as a string.
preg_match() or preg_match_all() do not actually take arrays as input, like preg_replace() does. You will need to loop over your array of expressions using something like foreach and applying an individual call to one of the matching functions to get your results.
I think preg_match_all() will suit your needs best, because it sounds like you're wanting to pull all of the matches out of the entire file, not just the first one. Once you have your full list of matches, then you'd apply your filter using the data from ignore.txt in a similar manner.
the quickest way would be to let the shell do the job
$result = `cat sample.html | egrep -f include.txt | egrep -vf ignore.txt`;
The input: we get some plain text as input string and we have to highlighight all URLs there with <a href={url}>{url></a>.
For some time I've used regex taken from http://flanders.co.nz/2009/11/08/a-good-url-regular-expression-repost/, which I modified several times, but it's built for another issue - to check whether the whole input string is an URL or no.
So, what regex do you use in such issues?
UPD: it would be nice if answers were related to php :-[
Take a look at a couple of modules available on CPAN:
URI::Find
URI::Find::Schemeless
where the latter is a little more forgiving. The regular expressions are available in the source code (the latter's, for example).
For example:
#! /usr/bin/perl
use warnings;
use strict;
use URI::Find::Schemeless;
my $text = "http://stackoverflow.com/users/251311/zerkms is swell!\n";
URI::Find::Schemeless
->new(sub { qq[$_[0]] })
->find(\$text);
print $text;
Output:
http://stackoverflow.com/users/251311/zerkms is swell!
For Perl, I usually use one of the modules defining common regex, Regexp::Common::URI::*. You might find a good regexp for you in the sources of those modules.
http://search.cpan.org/search?query=Regexp%3A%3ACommon%3A%3AURI&mode=module