The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 11:33:14 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Stripping unwanted characters in php  (Read 1861 times)
ekibastos
Rookie
**
Offline Offline

Posts: 38


View Profile
« on: December 02, 2008, 10:32:52 PM »

Code:

<?php
 
 $badchars 
= array("[""("")""]");
 
$notbadchars str_replace($badchars"""[(baseword:)]");

 echo 
$notbadchars ."<BR>";
 
?>



Could this be done faster?  Would a char by char c style
walkthrough copying to a new string be a better/faster/?
 method of cleaning/parsing strings?
Logged

<quote nutballs>
Apple is that hot chick that gives you lots of sex but is a total bitch with a horrible abusive temper.
</quote>
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: December 02, 2008, 11:00:20 PM »

I think that's about as good as it gets. I've tried some C-string style walking, but the problem is that you can only read chars in strings, you cannot set them. In other words, you can say $c = $str[37] but you cannot say $str[37] = chr(10)

And that is way faster than a preg_ function, so I think you're about tapped out there. You could use single quotes instead of doubles to kill string interpolation, that'd probably give you another 1-2 free cycles  Roll Eyes


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.
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #2 on: December 03, 2008, 04:28:20 AM »

php uses same regex as perl

In perl we can pre-compile the regex before doing iteration.

Code:
$regex  = qr/(\S+) hacker/;
$string = "Just another Perl hacker,";

if( $string =~ m/$regex/ )
    {
    print "I found a hacker of type $1\n";
    }

But I dont know if php has that same qr operator enabled (or wtf you call that)

Good luck
Bompa


Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: December 03, 2008, 07:11:24 AM »

hmmm... precompiled regex, that'd really be handy. You can do that in JS, have not really thought about it before. I'll bet that would ease the strain or PCRE functions a bit.

Nope Bomps, neither q or r are available as modifiers in PHP regex funcs.
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.
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!