Thread: boob qq
dink

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;
<?

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?

perkiset

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="([^"Applause"/';
$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

dink

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.

dink

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.

perkiset

quote author=dink link=topic=770.msg5320#msg5320 date=1202923004

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

php

 MyAdmining 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

perkiset

quote author=dink link=topic=770.msg5321#msg5321 date=1202923621

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 fish->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('fish', 'shit', 'Republican'), 'Bad doggie!', $inputString);

/p

dink

quote author=perkiset link=topic=770.msg5323#msg5323 date=1202923841

Are you talking about a cleanser, much like fish->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.  Fish.  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.


Perkiset's Place Home   Politics @ Perkiset's