【问题标题】:How to easily remove certain words from a text string? [duplicate]如何轻松地从文本字符串中删除某些单词? [复制]
【发布时间】:2022-02-07 18:01:16
【问题描述】:

希望大家身体健康!

我尝试了str_replace,但根本没有成功..!

我想要的是从整个文本中删除所有这些",因为我不想弄乱脚本,而这些 HTML 特殊字符真的弄乱了它并且它现在不起作用,因为这些太多了,所以如何用 "" nothing 替换所有这些:

#!/usr/bin/perl
# This script is NOT written or modified by me, I only copy pasted it from the internet.
# It was First originally Written by chudy_fernandez@yahoo.com
# & Have been modified by various persons over the net to fix/add various functions.
# Like For Example modified by member of comstuff.net to satisfy common and dynamic content.
# th30nly @comstuff.net a.k.a invisible_theater , Syaifudin JW , Ucok Karnadi and possibly other people too.
# For more info, http://wiki.squid-cache.org/ConfigExamples/DynamicContent/YouTube
# Syed Jahanzaib / aacable@hotmail.com
# http://aacable.wordpress.com/2012/01/19/youtube-caching-with-squid-2-7-using-storeurl-pl/
#######################
# Special thanks to some indonesian friends who provided some updates,
## UPDATED on 20 January, 2014 / Syed Jahanzaib

#####################
#### REFERENCES ##### http://www2.fh-lausitz.de/launic/comp/misc/squid/projekt_youtube/
#####################
#####################

use IO::File;
$|=1;
STDOUT->autoflush(1);
$debug=1; ## recommended:0
$bypassallrules=0; ## recommended:0
$sucks=""; ## unused
$sucks="sucks" if ($debug>=1);
$timenow="";
$printtimenow=1; ## print timenow: 0|1
my $logfile = '/tmp/storeurl.log';

【问题讨论】:

  • 哦,这么多文字很难阅读。
  • 那么,str_replace(array('&', 'amp;', 'quot;'), '', $s) 没有用吗? ideone.com/8qEvXx
  • 我根本不相信这是 PHP。请删除此问题的 PHP 标记。
  • @Nitin :我只想删除所有这些 HTML 特殊字符,不管哪个有效..! :D
  • 您不是在删除多个项目,而是在删除一个系列。另一个重复的 Srtubnexz 怎么样

标签: regex string


【解决方案1】:

你总是可以做这样的事情

查找:&(?:(?:amp|gt|lt|quot);)+
替换:''

编辑
对于额外的部分,一个 php 示例

$strNewSrc = preg_replace_callback('~http://([^;\r\n]+);~',
     function( $matches )
     {
         $tmp = $matches[1];
         $tmp = preg_replace('~\s+~', '', $tmp );
         $repl = "http://" + $tmp + ";";
         return $repl;
     },
     $strSrc
 );

【讨论】:

  • Great..Works in Sublime Text :D 此外,我还想删除像http:// . . . / . $y . . 这样的链接中的额外空格或空格,任何正则表达式都可以很好地实现这一点..!
  • 是的,在你运行上面之后,使用这个 - 使用一个通用的正则表达式替换回调。查找:http://([^;\r\n]+); 替换为 "http://" + RemoveWsp($1) + ";"
  • 这里是 Pastebin 更清晰的例子:pastebin.com/icBRa9t4
  • 我上面的建议是将75.$out=http://www.fileserve.com.SQUIDINTERNAL/ . $1 . ./ . $2 . ; 变成75.$out=http://www.fileserve.com.SQUIDINTERNAL/.$1../.$2.; 但是,它需要一个回调功能,这在文本编辑器应用程序中可能不可用。
  • 应用上面的正则表达式后,我的脚本变成了这个..它搞砸了一切..! :pastebin.com/bhwDrNXD
【解决方案2】:

此处注释的步骤可以帮助您实现这一目标:

<?php

    // GET & DUMP THE CONTENTS OF THE FILE INTO A VARIABLE: $fileContents
    $fileContents   = file_get_contents(__DIR__ . "/_data_1.txt");

    // CLEAN UP THE TEXTS (REMOVING: quot; AND OR amp; AND OR &amp;)
    $cleanText      = preg_replace("#((quot\;)*?|(\&amp\;)|(amp\;)*?)#si", "", $fileContents);

    // RE-SAVE THE FILE
    file_put_contents(__DIR__ . "/_data_2.txt", $cleanText);
    var_dump($cleanText);

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 2021-05-22
    • 2017-06-16
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多