Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Perl -> PHP

Perl -> PHP
Thread Tools
Jaey
Mac Elite
Join Date: Dec 2003
Status: Offline
Reply With Quote
Feb 23, 2004, 06:06 PM
 
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
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Feb 23, 2004, 08:12 PM
 
[php]

$censored = "foo|bar|foobar|americanidolsingers";

$text = preg_replace("!(?b)(".$censored.")(?b)!msi","* ***",$text);

[/php]
Computer thez nohhh...
     
Jaey  (op)
Mac Elite
Join Date: Dec 2003
Status: Offline
Reply With Quote
Feb 23, 2004, 08:31 PM
 
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. )
     
Jaey  (op)
Mac Elite
Join Date: Dec 2003
Status: Offline
Reply With Quote
Feb 23, 2004, 11:24 PM
 
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
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Feb 24, 2004, 12:33 AM
 
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...
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Feb 24, 2004, 12:43 AM
 
[Double-post]
Computer thez nohhh...
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:41 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,