|
|
Perl -> PHP
|
|
|
|
Mac Elite
Join Date: Dec 2003
Status:
Offline
|
|
I wrote a script in perl that effectively censors out certain words. I was hoping to translate this script into php, but even after reading the docs at php.net I got some errors. So, I was hoping someone could translate it for me (and/or simplify it if possible).
Code:
#!/usr/bin/perl -w
$strings=<>;
@tstrings=split(" ", $strings);
for($i=0; $i<=$#tstrings; $i++)
{
if($tstrings[$i] eq "for" || $tstrings[$i] eq "bar")
{
$tstrings[$i]="*****";
}
}
$nstrings=join(" ", @tstrings);
print "$nstrings\n";
-thanks
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
[php]
$censored = "foo|bar|foobar|americanidolsingers";
$text = preg_replace("!(? b)(".$censored.")(? b)!msi","* ***",$text);
[/php]
|
Computer thez nohhh...
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Dec 2003
Status:
Offline
|
|
That doesn't seem to... work. It doesn't replace anything...
EDIT: After reading the docs on preg_replace I got it fixed and working.. thanks for the help
(
Last edited by Jaey; Feb 23, 2004 at 08:38 PM.
)
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Dec 2003
Status:
Offline
|
|
Actually, could you explain the !(?:b) stuff? I had to trash that to get it to work, but I'm not sure what it does. Thanks
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
My bad, I should have tested it first (but I thought I'd take the Microsoft attitude of getting everyone else to beta test for me )
The '!' were simply meant to be delimeters for the GREP pattern - I could have used any old character there.
The ? b is meant to match a sub-pattern without actually including it in the captured result. So for instance with the following:-
preg_replace('!((?:HELLO)THERE)!msi','\\1','HELLOT HERE')
the search string matches 'HELLOTHERE' but only 'THERE' is captured and used as the replacement subpattern....
Actually, come to think of it, I didn't even need it in the example I gave up top. Ooops - sorry! The reason I was using it was to use the '\b' (or word boundary character) as a means of checking that the full word was matched, but not to include that boundary when it was replaced. What I SHOULD have written was:-
preg_replace('!?<=\b(".$censored.")?=\b!msi","**** ",$text);
...which uses assertions. It means that it should find a word boundary BEFORE ( ?<\b ) then the string you want to find, then another word boundary AFTER ( ?=\b ) but not to capture those word boundaries in the match.
The grep manual is full of very cool stuff - I just learned how to use the recursion modifier to match balanced tags within an RTF file, but it's taken me a week or so to fully get my head around it.
I can see why the true web geeks go for PERL, as it really does some powerful stuff - it's just that my poor brain is better suited to the less cryptic PHP for day-to-day stuff.
(
Last edited by Simon Mundy; Feb 24, 2004 at 12:44 AM.
)
|
Computer thez nohhh...
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
|
Computer thez nohhh...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|