Jump to content

preg_match that removes all the special chars & non-url ones


Recommended Posts

I've got already one that does it, but it stops after "=" sign. How to deal with it (that the function would not stop?)

 

function _url($name) { # parse url
$sign = '-'; $name = str_replace('"','',$name); $name = preg_replace("/[^a-zA-Z0-9\s]/","",$name); $name = str_replace(' ',$sign,$name); $name = str_replace($sign.$sign,$sign,$name); $name = str_replace($sign.$sign.$sign,$sign,$name); $name = str_replace($sign.$sign.$sign,$sign.$sign,$name);
return $name;
}

function _url($name,$lenght='') { /* parse URL */
if(strlen($name) > $lenght) { $name = substr($name,0,$titleLenght); $tiny = 1; }

$sign = '-'; $name = unhtmlspecialchars($name); $name = str_replace('-',' ',$name); $name = preg_replace("/[^a-zA-Z0-9\s]/","",$name); $name = str_replace(' ',$sign,$name);
$name = str_replace($sign.$sign,$sign,$name); $name = str_replace($sign.$sign.$sign,$sign,$name); $name = str_replace($sign.$sign.$sign,$sign,$name); $name = str_replace($sign.$sign.$sign.$sign,$sign,$name);

if($tiny == 1) { if(strpos($name,'-') !== false) $name = substr($name,0,strrpos($name,'-')); if(substr($name,-1) == $sign) $name = substr($name,0,-1); }
if(substr($name,-1) == '-') $name = substr($name,0,-1); if(substr($name,-1) == '-') $name = substr($name,0,-1); if(substr($name,-1) == '-') $name = substr($name,0,-1); if(substr($name,-1) == '-') $name = substr($name,0,-1);

return $name;
}

fixed. think that final; put it, because it might be useful for somebody.

 

function _url($name,$lenght='') { /* parse URL */
if(strlen($name) > $lenght) { $name = substr($name,0,$lenght); $tiny = 1; }

$sign = '-'; $name = unhtmlspecialchars($name); $name = str_replace('-',' ',$name); $name = preg_replace("/[^a-zA-Z0-9\s]/","",$name); $name = str_replace(' ',$sign,$name);
for($i = 0; $i <= 10; $i++) $name = str_replace($sign.$sign,$sign,$name);

if($tiny == 1) { if(strpos($name,'-') !== false) $name = substr($name,0,strrpos($name,'-')); if(substr($name,-1) == $sign) $name = substr($name,0,-1); }
for($i = 0; $i <= 10; $i++) if(substr($name,-1) == '-') $name = substr($name,0,-1);

return $name;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.