sql >> Database teknologi >  >> RDS >> Mysql

fjern specialtegn i php

Easy peasy:

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

Brug:

echo clean('a|"[email protected]£de^&$f g');

Vil udlæse:abcdef-g

Rediger :

Hey, just a quick question, how can I prevent multiple hyphens from being next to each other? and have them replaced with just 1? Thanks in advance!

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

   return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}

henvis dette link



  1. Skinner med Postgres-data returneres ude af drift

  2. Regler for implementering af TDD i gammelt projekt

  3. hvordan man implementerer en kompliceret sql-kommando

  4. Brug for tidseffektiv metode til at importere stor CSV-fil via PHP til flere MySQL-tabeller