Welcome Guest, Not a member yet? Register   Sign In
Strip out non-permitted characters
#1

[eluser]Phil Sturgeon[/eluser]
Never been a fan of regex. Its powerful stuff by just that little too good for my brain at times.

Basically im looking for a way to replace any symbols or characters that are NOT in $this->config->item('permitted_uri_chars');

Got as far as...

Code:
$url_friendly = ereg_replace("[^[:alnum:]+]", '_', trim($naughty_string));
#2

[eluser]gtech[/eluser]
Hello

try this.. I knicked the regular expression from the router library and adapted it.

Code:
<?php
class Blog extends Controller {

  function index()
  {
    $str = 'Hello World! ** %_ 09 @#@#@#';
    $newStr = preg_replace("/[^".preg_quote($this->config->item('permitted_uri_chars'))."]/i",' ', $str);

    echo $newStr;
    echo "<br>";
    echo $str;
  }
}
?&gt;

it replaces non permitted chars with a space but you can change it a blank if you want, it seems to work for every example i have chucked at it. The obvious advantage of doing it this way is that it pulls the permitted characters directly from config.

result:

Code:
Hello World %_ 09
Hello World! ** %_ 09 @#@#@#
#3

[eluser]ejangi[/eluser]
P.S. ereg functions are being prepared for deprecation in PHP 6 (though they'll likely still be around till PHP 7), so for future-proofing sake, use preg where you can. ;-)




Theme © iAndrew 2016 - Forum software by © MyBB