preg_replace oder alternative
lima-city → Forum → Programmiersprachen → PHP, MySQL & .htaccess
array
bestimmten auswahl
code
entfernen
enthaltenen zeichen
ergebnis
erlaubte zeichen
folgendem muster
klappen
sinn
string
versuchen
vorherige ergebnis
vorkommen
zeichen
-
Moin moin!
Ich möchte einen String auf erlaubte Zeichen überprüfen und die verbotenen rauslöschen.
$gsm_charset = array( "0x0A" => array("hexGSM"=>"0x0A" , "len"=>1),"0x0C" => array("hexGSM"=>"0x1B0A", "len"=>2),"0x0D" => array("hexGSM"=>"0x0D", "len"=>1),"0x20" => array("hexGSM"=>"0x20", "len"=>1),"0x21" => array("hexGSM"=>"0x21", "len"=>1),"0x22" => array("hexGSM"=>"0x22", "len"=>1),"0x23" => array("hexGSM"=>"0x23", "len"=>1),"0x24" => array("hexGSM"=>"0x02", "len"=>1),"0x25" => array("hexGSM"=>"0x25", "len"=>1),"0x26" => array("hexGSM"=>"0x26", "len"=>1),"0x27" => array("hexGSM"=>"0x27", "len"=>1),"0x28" => array("hexGSM"=>"0x28", "len"=>1),"0x29" => array("hexGSM"=>"0x29", "len"=>1),"0x2A" => array("hexGSM"=>"0x2A", "len"=>1),"0x2B" => array("hexGSM"=>"0x2B", "len"=>1),"0x2C" => array("hexGSM"=>"0x2C", "len"=>1),"0x2D" => array("hexGSM"=>"0x2D", "len"=>1),"0x2E" => array("hexGSM"=>"0x2E", "len"=>1),"0x2F" => array("hexGSM"=>"0x2F", "len"=>1),"0x30" => array("hexGSM"=>"0x30", "len"=>1),"0x31" => array("hexGSM"=>"0x31", "len"=>1),"0x32" => array("hexGSM"=>"0x32", "len"=>1),"0x33" => array("hexGSM"=>"0x33", "len"=>1),"0x34" => array("hexGSM"=>"0x34", "len"=>1),"0x35" => array("hexGSM"=>"0x35", "len"=>1),"0x36" => array("hexGSM"=>"0x36", "len"=>1),"0x37" => array("hexGSM"=>"0x37", "len"=>1),"0x38" => array("hexGSM"=>"0x38", "len"=>1),"0x39" => array("hexGSM"=>"0x39", "len"=>1),"0x3A" => array("hexGSM"=>"0x3A", "len"=>1),"0x3B" => array("hexGSM"=>"0x3B", "len"=>1),"0x3C" => array("hexGSM"=>"0x3C", "len"=>1),"0x3D" => array("hexGSM"=>"0x3D", "len"=>1),"0x3E" => array("hexGSM"=>"0x3E", "len"=>1),"0x3F" => array("hexGSM"=>"0x3F", "len"=>1),"0x40" => array("hexGSM"=>"0x00", "len"=>1),"0x41" => array("hexGSM"=>"0x41", "len"=>1),"0x42" => array("hexGSM"=>"0x42", "len"=>1), "0x43" => array("hexGSM"=>"0x43", "len"=>1),"0x44" => array("hexGSM"=>"0x44", "len"=>1),"0x45" => array("hexGSM"=>"0x45", "len"=>1),"0x46" => array("hexGSM"=>"0x46", "len"=>1),"0x47" => array("hexGSM"=>"0x47", "len"=>1),"0x48" => array("hexGSM"=>"0x48", "len"=>1),"0x49" => array("hexGSM"=>"0x49", "len"=>1),"0x4A" => array("hexGSM"=>"0x4A", "len"=>1),"0x4B" => array("hexGSM"=>"0x4B", "len"=>1),"0x4C" => array("hexGSM"=>"0x4C", "len"=>1),"0x4D" => array("hexGSM"=>"0x4D", "len"=>1),"0x4E" => array("hexGSM"=>"0x4E", "len"=>1),"0x4F" => array("hexGSM"=>"0x4F", "len"=>1),"0x50" => array("hexGSM"=>"0x50", "len"=>1),"0x51" => array("hexGSM"=>"0x51", "len"=>1),"0x52" => array("hexGSM"=>"0x52", "len"=>1),"0x53" => array("hexGSM"=>"0x53", "len"=>1),"0x54" => array("hexGSM"=>"0x54", "len"=>1),"0x55" => array("hexGSM"=>"0x55", "len"=>1),"0x56" => array("hexGSM"=>"0x56", "len"=>1),"0x57" => array("hexGSM"=>"0x57", "len"=>1),"0x58" => array("hexGSM"=>"0x58", "len"=>1),"0x59" => array("hexGSM"=>"0x59", "len"=>1),"0x5A" => array("hexGSM"=>"0x5A", "len"=>1),"0x5B" => array("hexGSM"=>"0x1B3C", "len"=>2),"0x5C" => array("hexGSM"=>"0x1B2F", "len"=>2),"0x5D" => array("hexGSM"=>"0x1B3E", "len"=>2),"0x5E" => array("hexGSM"=>"0x1B14", "len"=>2),"0x5F" => array("hexGSM"=>"0x11", "len"=>1),"0x61" => array("hexGSM"=>"0x61", "len"=>1),"0x62" => array("hexGSM"=>"0x62", "len"=>1),"0x63" => array("hexGSM"=>"0x63", "len"=>1),"0x64" => array("hexGSM"=>"0x64", "len"=>1),"0x65" => array("hexGSM"=>"0x65", "len"=>1),"0x66" => array("hexGSM"=>"0x66", "len"=>1),"0x67" => array("hexGSM"=>"0x67", "len"=>1),"0x68" => array("hexGSM"=>"0x68", "len"=>1),"0x69" => array("hexGSM"=>"0x69", "len"=>1), "0x6A" => array("hexGSM"=>"0x6A", "len"=>1),"0x6B" => array("hexGSM"=>"0x6B", "len"=>1),"0x6C" => array("hexGSM"=>"0x6C", "len"=>1),"0x6D" => array("hexGSM"=>"0x6D", "len"=>1),"0x6E" => array("hexGSM"=>"0x6E", "len"=>1),"0x6F" => array("hexGSM"=>"0x6F", "len"=>1),"0x70" => array("hexGSM"=>"0x70", "len"=>1),"0x71" => array("hexGSM"=>"0x71", "len"=>1),"0x72" => array("hexGSM"=>"0x72", "len"=>1),"0x73" => array("hexGSM"=>"0x73", "len"=>1),"0x74" => array("hexGSM"=>"0x74", "len"=>1),"0x75" => array("hexGSM"=>"0x75", "len"=>1),"0x76" => array("hexGSM"=>"0x76", "len"=>1),"0x77" => array("hexGSM"=>"0x77", "len"=>1),"0x78" => array("hexGSM"=>"0x78", "len"=>1),"0x79" => array("hexGSM"=>"0x79", "len"=>1),"0x7A" => array("hexGSM"=>"0x7A", "len"=>1),"0x7B" => array("hexGSM"=>"0x1B28", "len"=>2),"0x7C" => array("hexGSM"=>"0x1B40", "len"=>2),"0x7D" => array("hexGSM"=>"0x1B29", "len"=>2),"0x7E" => array("hexGSM"=>"0x1B3D", "len"=>2),"0xC2A0" => array("hexGSM"=>"0x1B", "len"=>1),"0xC2A1" => array("hexGSM"=>"0x40", "len"=>1),"0xC2A3" => array("hexGSM"=>"0x01", "len"=>1),"0xC2A4" => array("hexGSM"=>"0x24", "len"=>1),"0xC2A5" => array("hexGSM"=>"0x03", "len"=>1),"0xC2A7" => array("hexGSM"=>"0x5F", "len"=>1),"0xC2BF" => array("hexGSM"=>"0x60", "len"=>1),"0xC384" => array("hexGSM"=>"0x5B", "len"=>1),"0xC385" => array("hexGSM"=>"0x0E", "len"=>1),"0xC386" => array("hexGSM"=>"0x1C", "len"=>1),"0xC389" => array("hexGSM"=>"0x1F", "len"=>1),"0xC391" => array("hexGSM"=>"0x5D", "len"=>1),"0xC396" => array("hexGSM"=>"0x5C", "len"=>1),"0xC398" => array("hexGSM"=>"0x0B", "len"=>1),"0xC39C" => array("hexGSM"=>"0x5E", "len"=>1),"0xC39F" => array("hexGSM"=>"0x1E", "len"=>1), "0xC3A0" => array("hexGSM"=>"0x7F", "len"=>1),"0xC3A4" => array("hexGSM"=>"0x7B", "len"=>1),"0xC3A5" => array("hexGSM"=>"0x0F", "len"=>1),"0xC3A6" => array("hexGSM"=>"0x1D", "len"=>1),"0xC3A7" => array("hexGSM"=>"0x09", "len"=>1),"0xC3A8" => array("hexGSM"=>"0x04", "len"=>1),"0xC3A9" => array("hexGSM"=>"0x05", "len"=>1),"0xC3AC" => array("hexGSM"=>"0x07", "len"=>1),"0xC3B1" => array("hexGSM"=>"0x7D", "len"=>1),"0xC3B2" => array("hexGSM"=>"0x08", "len"=>1),"0xC3B6" => array("hexGSM"=>"0x7C", "len"=>1),"0xC3B8" => array("hexGSM"=>"0x0C", "len"=>1),"0xC3B9" => array("hexGSM"=>"0x06", "len"=>1),"0xC3BC" => array("hexGSM"=>"0x7E", "len"=>1),"0xCE93" => array("hexGSM"=>"0x13", "len"=>1),"0xCE94" => array("hexGSM"=>"0x10", "len"=>1),"0xCE98" => array("hexGSM"=>"0x19", "len"=>1),"0xCE9B" => array("hexGSM"=>"0x14", "len"=>1),"0xCE9E" => array("hexGSM"=>"0x1A", "len"=>1),"0xCEA0" => array("hexGSM"=>"0x16", "len"=>1),"0xCEA3" => array("hexGSM"=>"0x18", "len"=>1),"0xCEA6" => array("hexGSM"=>"0x12", "len"=>1),"0xCEA8" => array("hexGSM"=>"0x17", "len"=>1),"0xCEA9" => array("hexGSM"=>"0x15", "len"=>1),"0xE282AC" => array("hexGSM"=>"0x1B65", "len"=>2) ); function trim($str, $max_strlen = 160) { foreach($this->gsm_charset AS $ekey => $value){ $data = "\x{".substr($ekey,2)."}"; $pattern ='/'.$data.'/iu'; $str2 .= preg_replace($pattern,"$1",$str); } $strlen = $this->strlen($str2); while($strlen>$max_strlen) { $str2 = mb_substr($str2, 0, -1,"UTF-8"); $strlen = $this->strlen($str2, $max_strlen); } return array( 'str' => $str, 'strlen' => $strlen ); }
Ich meine es hätte vorher geklappt, oder ob es nur mein JS war das es klappen lassen hat, ich weiß es nicht.
Auf jeden Fall werden die nicht als KEY enthaltenen Zeichen nicht entfernt, was sie eigentlich sollten... -
Diskutiere mit und stelle Fragen: Jetzt kostenlos anmelden!
lima-city: Gratis werbefreier Webspace für deine eigene Homepage
-
Hallo,
habe ich dich richtig verstanden, du willst aus dem String $str alle Zeichen entfernen, die nicht im Array $gsm_charset als key vorkommen? Wie sieht denn der String $str aus? Derzeit suchst du z.B. nach dem String \x{0A} und ersetzt dies durch durch $1, dann weiter mit \x{0C} und dieses Ergebnis hängst du an das vorherige Ergebnis dran¿
Der Sinn des Ganzen, bezogen auf die Ausgangsfrage erschließt sich mir bestenfalls ansatzweise.
Um alle Zeichen, ausser einer bestimmten Auswahl zu entfernen würde ich preg_replace($pattern,"",$str) mit folgendem Muster versuchen: $pattern /[^<hier die Zeichen, die bleiben sollen>]/ versuchen, ist aber ungetestet.
Gruß -
Diskutiere mit und stelle Fragen: Jetzt kostenlos anmelden!
lima-city: Gratis werbefreier Webspace für deine eigene Homepage