【问题标题】:removing characters from array after sorting in php在php中排序后从数组中删除字符
【发布时间】:2018-02-08 06:08:54
【问题描述】:

我在 PHP 中按字母顺序从一个运行良好的文本文件中对数据进行排序,但不幸的是,自动填充的文本文件包含 #039 之类的字符;我想从最终结果中删除。尝试了很多方法来替换和删除字符,但没有成功。这是我到目前为止所拥有的:

    <?php
error_reporting(E_ALL);

$fileName = 'cache/_city.txt';
$data     = file_get_contents($fileName);

// Assuming that the file had the data in one line...

// Split the data using the delimiter
$split = explode("|", $data);

// Sort
sort($split);

// Put it all back together
$data = implode("&nbsp", $split);
$data = str_replace("&#039;" , "", $data);

echo $data;

?> 

如何从 $data 中删除这段文本:#039;

样本数据:

<a href="?city=Leiden">Leiden</a>|
<a href="?city=Papendrecht">Papendrecht</a>|
<a href="?city=Helmond">Helmond</a>|
<a href="?city=%26%23039%3Bs-Hertogenbosch">&amp;#039;s-Hertogenbosch</a>|
<a href="?city=Hengelo">Hengelo</a>|
<a href="?city=Marknesse">Marknesse</a>|
<a href="?city=Wanssum">Wanssum</a>|
<a href="?city=Rijswijk">Rijswijk</a>|
<a href="?city=Leunen">Leunen</a>|
<a href="?city=Genemuiden">Genemuiden</a>|

【问题讨论】:

  • “尝试了很多方法来替换和删除字符”请证明
  • 虽然您的问题似乎是要删除哪些字符,但也可以根据您要保留的字符/字符串来制定。这可以更简单地想象一个可以在 preg_grep 中使用的正则表达式。
  • 您需要删除#039; 或&amp;#039; 吗?
  • $data 样本怎么样?
  • 添加了示例数据

标签: php strtr


【解决方案1】:

你有没有尝试过这样的事情:

$data = str_replace($wrongChar , "", $data);

编辑:

即使我认为你会“清理”比你需要的更多,你能测试一下吗:

$data = file_get_contents($fileName);
$data = preg_replace('/[^A-Za-z0-9\-]/', '', $data);

第二版:

知道 *_replace 有效,我改进了一点我的建议。

<?php

error_reporting(E_ALL);

// It will apply html_entity_decode serveral times on the string to convert all HTML entities
$recursive_decode = function($str, $depth = 1) use (&$recursive_decode) {
    if (!$depth) {
        return $str;
    }

    return $recursive_decode(html_entity_decode($str, ENT_QUOTES, 'UTF-8'), --$depth);
};

$fileName = 'cache/_city.txt';

// In this test, try with a depth egals to 2
$data     = $recursive_decode(file_get_contents($fileName), 2);

// Assuming that the file had the data in one line...

// Split the data using the delimiter
$split = explode('|', $data);

// Sort
sort($split);

// Put it all back together
$data = implode("&nbsp", $split);

// Because recursive_decode converted all entities, your previous "&#039" is now "'"
$data = str_replace("'" , "", $data);

echo $data;

?>

【讨论】:

  • 我试过了,但没用,我还更新了我的问题以使其更清楚。
  • 它确实清理了比需要更多的东西并弄乱了整个输出
  • 好的。所以至少 preg_replace / str_replace 策略是有效的。我认为您可以专注于如何达到#039;。正如 Ynhockey 所说,尝试查看 urldecode 函数
  • 我进行了第二次编辑,希望对您有所帮助
  • 最后的编辑工作很成功。非常感谢。
【解决方案2】:

问题中没有足够的信息说明您要替换的内容。这将基本上确定答案。

如果您只想替换几个特定字符,最好使用str_replace 或其变体,但如果它是多个“垃圾”字符(在您的答案中暗示),您可以替换 Unicode范围(使用preg_replace),例如。有人问,这里得到了简单的回答:How do I replace characters not in range [0x5E10, 0x7F35] with '*' in PHP?

函数参考:

https://secure.php.net/manual/en/function.str-replace.php https://secure.php.net/manual/en/function.preg-replace.php

旁注:您应该使用&amp;nbsp;,而不是&amp;nbsp。

编辑:根据您提供的新信息,您似乎正在尝试删除已编码的字符,请尝试:str_replace('&amp;#039;', '', urldecode($data))

【讨论】:

    猜你喜欢
    • 2011-05-06
    • 2013-11-05
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    相关资源
    最近更新 更多