试图在php中删除字符串中的重音符号

时间:2016-04-15 20:21:16

标签: php

我正在尝试从字符串中删除重音并在php中编写以下函数,但它不会删除它们。字符串带有重音符,并带有重音符。

function stripaccents($string)
{
    $replace = array('é','í','ó','á','ñ', 'ú', 'ü');
    $with = array('e','i','o','a', 'n', 'u', 'u');

    foreach ($replace as &$char)
    {
        $char = utf8_decode($char);
    }
    unset($char);

    $newstring = str_replace($replace, $with, $string);

    return $newstring;
}

5 个答案:

答案 0 :(得分:2)

您也可以使用iconv

$newstring = iconv('UTF-8', 'ASCII//TRANSLIT', $string);

答案 1 :(得分:1)

如果您使用的是PHP 5.3或更高版本(您应该使用),请使用内置函数:

$newstring = normalizer_normalize($oldstring);

答案 2 :(得分:0)

由于您正在寻找特定的西班牙语字符,因此您可以使用正则表达式来查找这些特定字符:

function stripaccents($string)
{
    $replace = array('/é/','/í/','/ó/','/á/','/ñ/', '/ú/', '/ü/');
    $with = array('e','i','o','a', 'n', 'u', 'u');

    $newstring = preg_replace($replace, $with, $string);

    return $newstring;
}

否则,您可以使用已经提到的其他功能之一。

答案 3 :(得分:0)

我认为你不需要

foreach ($replace as &$char)
    {
        $char = utf8_decode($char);
    }
    unset($char);

因为utf8_decode尝试将2字节值从 $ replace 转换为1字节值

答案 4 :(得分:0)

这是我正在使用的自定义。

Error:(31, 41) java: no suitable constructor found for RuntimeOptions(java.util.Properties,java.lang.String[])
    constructor cucumber.runtime.RuntimeOptions.RuntimeOptions(cucumber.runtime.Env,java.util.List<java.lang.String>) is not applicable
      (argument mismatch; java.util.Properties cannot be converted to cucumber.runtime.Env)
    constructor cucumber.runtime.RuntimeOptions.RuntimeOptions(cucumber.runtime.formatter.PluginFactory,java.util.List<java.lang.String>) is not applicable
      (argument mismatch; java.util.Properties cannot be converted to cucumber.runtime.formatter.PluginFactory)
Error:(33, 27) java: no suitable constructor found for Runtime(cucumber.runtime.io.MultiLoader,java.lang.ClassLoader,cucumber.runtime.RuntimeOptions)
    constructor cucumber.runtime.Runtime.Runtime(cucumber.runtime.io.ResourceLoader,cucumber.runtime.ClassFinder,java.lang.ClassLoader,cucumber.runtime.RuntimeOptions) is not applicable
      (actual and formal argument lists differ in length)
    constructor cucumber.runtime.Runtime.Runtime(cucumber.runtime.io.ResourceLoader,java.lang.ClassLoader,java.util.Collection<? extends cucumber.runtime.Backend>,cucumber.runtime.RuntimeOptions) is not applicable
      (actual and formal argument lists differ in length)
    constructor cucumber.runtime.Runtime.Runtime(cucumber.runtime.io.ResourceLoader,java.lang.ClassLoader,java.util.Collection<? extends cucumber.runtime.Backend>,cucumber.runtime.RuntimeOptions,cucumber.runtime.RuntimeGlue) is not applicable
      (actual and formal argument lists differ in length)
    constructor cucumber.runtime.Runtime.Runtime(cucumber.runtime.io.ResourceLoader,java.lang.ClassLoader,java.util.Collection<? extends cucumber.runtime.Backend>,cucumber.runtime.RuntimeOptions,cucumber.runtime.StopWatch,cucumber.runtime.RuntimeGlue) is not applicable
      (actual and formal argument lists differ in length)

Error:(35, 20) java: cannot find symbol
  symbol:   method writeStepdefsJson()
  location: variable runtime of type cucumber.runtime.Runtime