The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 16, 2019, 09:20:15 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: boob qq  (Read 4138 times)
dink
Expert
****
Offline Offline

Posts: 349


View Profile
« on: February 13, 2008, 01:41:02 AM »

My latest project involves a little danza con el diablo in the form of user input.  To minimize the asswhoopin that my server is likely to take, I decided to do some replacement on the user submitted data.

I have an array of words that I want to transmorgrify.  Here is what I have;
Code:
<?php

$replace 
= array(
" orig1 " => " repl1 ",
" orig2 " => " repl2 ",
" orig3 " => " repl3 ",
" orig4 " => " repl4 ",
.
.
.
.
);

function 
clean_text$input ) {

$input preg_replace(array_keys($replace), array_values($replace), $input);
print $input;
}

?>


Guess what?  It doesn't do it's thang.  The input remains the same as the output.

What did I do wrong?  Let me qualify that, I know that it's wrong for me to try to code.  And, I shouldn't play around in the code playground, but it has to be done.  So where did I boob this one up?
Logged

[quote Nutballs]
the universe has a giant fist, and its got enough whoop ass for everyone.
[/quote]
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: February 13, 2008, 08:03:43 AM »

Heyya Dink -

Here's the manual on preg_replace: http://us3.php.net/manual/en/function.preg-replace.php

but we can do this quickly. When using an array for the replacement, you don't do it in a keyed array as you have - you specify the originals in one array and the replacements in the next. This is most easily demonstrated with the str_replace before preg_replace:

$search = array('cat', 'dog', 'mouse');
$replace = array('kitten', 'puppy', 'mouselet');
$newBuff = str_replace($search, $replace, $inputString);

It's just a little stranger looking with regex functions:

$search = array('/cat/', '/dog/', '/mouse/');
$replace = array('kitten', 'puppy', 'mouselet');
$newBuff = str_replace($search, $replace, $inputString);

The reason you use preg_replace is if you need really different replacements, like changing a graphic in a URL:

$search[] = '/img src="([^")"/';
$replace[] = 'img src="$1"';
$newBuff = preg_replace($search, $replace, $inputStr);

Note that I only did one element in the arrays this time - if this were the case you wouldn't need to use arrays at all.

Gotta run and drop the kids off at school... pingback if'n you be needing more.

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
dink
Expert
****
Offline Offline

Posts: 349


View Profile
« Reply #2 on: February 13, 2008, 10:16:44 AM »

Rofl.  Whaddadumbass. 

Thanks Perk.

There ain't no regex involved, so how in the hell did I expect a preg_anything() to work?

I guess when I was chasing the elusive preg_replace_all() in the function above this one in the class. 

Completely off topic, but there are a ton of search results where someone says "just use preg_replace_all().  Talk about your ordinary 'snipe hunt'.

Alrighty.  I have the replace array hard coded in this function.  After testing, I want to remove it a separate file so it can be updated without trashing the class.  So, if I use your example I'll have two arrays to work with. 

Would it make life simpler to load the conditional data into a database and flop around in it there?

Off to break my server.  Again.
Logged

[quote Nutballs]
the universe has a giant fist, and its got enough whoop ass for everyone.
[/quote]
dink
Expert
****
Offline Offline

Posts: 349


View Profile
« Reply #3 on: February 13, 2008, 10:27:01 AM »

Edit...

I just re read this line:
Quote
Note that I only did one element in the arrays this time - if this were the case you wouldn't need to use arrays at all.

Hmmmm.  Not sure what you mean.  What I have is user supplied input that may, or may not, have some things that I don't want to see.  I want to run <something> against the input to change what I don't want into what will be acceptable.  Then process the cleansed input elsewhere.

What I imagined would happen is that the <something> would run thru the input word-by-word, changing 'bad stuff' into 'good stuff'.

Imagination can be a scary thing to have around the house.
Logged

[quote Nutballs]
the universe has a giant fist, and its got enough whoop ass for everyone.
[/quote]
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #4 on: February 13, 2008, 10:27:26 AM »

Alrighty.  I have the replace array hard coded in this function.  After testing, I want to remove it a separate file so it can be updated without trashing the class.  So, if I use your example I'll have two arrays to work with. 

Would it make life simpler to load the conditional data into a database and flop around in it there?

Really depends on how complicated your life gets when you add a DB. If you don't need or are unfamiliar with phpMyAdmining a new table to handle this stuff, then files are fine. The code would be as simple as:

$search = explode(chr(10), file_get_contents('/adir/searchStrings.txt'));
$replace = explode(chr(10), file_get_contents('/adir/replaceStrings.txt'));
$newBuff = str_replace($search, $replace, $inputStr);

of course you could even do that in just one line if you were so inclined. With MySQL you'd need to do things slightly differently. You'd probably have a table where each row had a search and replace field... and your code would look something like:

$set = mysql_query("select searchstr, replacestr from replacements");
while ($theRow = mysql_fetch_row($set))
{
   $search[] = $theRow[0];
   $replace[] = $theRow[1];
}
$newBuff = str_replace($search, $replace, $inputStr);

As you can see, not terribly difficult, just two different ways of thinking.

Nice to have you around my friend,
/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: February 13, 2008, 10:30:41 AM »

What I imagined would happen is that the <something> would run thru the input word-by-word, changing 'bad stuff' into 'good stuff'.

Are you talking about a cleanser, much like fuck->fish @ syndk8? Then you'd use arrays because you'd probably have several cleansing tasks, and to do them all in a single PHP function call with multiple things to replace is the fastest way you're going to do it. If in every case you're going to convert all bad words to "[clipped]" or something, then the $search side can be an array and the $replace side can be a single string, "[clipped]". For example:

$newBuff = str_replace(array('fuck', 'shit', 'Republican'), 'Bad doggie!', $inputString);

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
dink
Expert
****
Offline Offline

Posts: 349


View Profile
« Reply #6 on: February 13, 2008, 12:58:13 PM »

Are you talking about a cleanser, much like fuck->fish @ syndk8? Then you'd use arrays because you'd probably have several cleansing tasks, and to do them all in a single PHP function call with multiple things to replace is the fastest way you're going to do it.
/p

Yep, that's precisely what I had in mind.  Many cleaning tasks.

My first attempt at this was a flipping disaster.  Had about 15 lines of code where I was exploding this, trimming spaces, removing punctuation, imploding that, concatenating something else.  Fuck.  Makes my head hurt even thinking about how complicated it was getting.

Guess I'd better get busy slicing, dicing, and walking the array doggie.

Thanks again Perk.
Logged

[quote Nutballs]
the universe has a giant fist, and its got enough whoop ass for everyone.
[/quote]
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!