- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我需要清理包含 PHP 序列化值的 mysql 转储(我使用 sed 搜索/替换 url)
在 PHP 中我会这样做:
<?php
$ret_string = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.mb_strlen('$2').':\"$2\";'", $string );
?>
你会如何在 bash 中做到这一点?
示例原始字符串:
a:3:{s:7:"string1";s:4:"test";s:3:"url";s:17:"http://myurl.com";s:7:"string2";s:4:"test";}
我已经用 mynewurl.com 替换了 myurl.com,所以它现在看起来像:
a:3:{s:7:"string1";s:4:"test";s:3:"url";s:17:"http://mynewurl.com";s:7:"string2";s:4:"test";}
我需要更改的是字符串的长度,以反射(reflect)新字符串 (s:17) 的长度,以便最终字符串变为:
a:3:{s:7:"string1";s:4:"test";s:3:"url";s:19:"http://mynewurl.com";s:7:"string2";s:4:"test";}
我的 sql 转储中有很多这样的文件,它们是文件系统上的一个文件。虽然域名变了,但是url可能有额外的路径,因此字符串长度不同
最佳答案
在正则表达式中这样做的问题是正则表达式不适合结构化文本,如 JSON 或 PHP 序列化字符串。如果您知道您的输入数据将始终遵循某种结构,您就可以“伪造”事物,但是随着时间的推移,这样编写的代码会出现问题。事情总是随着时间而改变。如果您可以避免这种黑客行为,最好这样做。
我们到底想解决什么问题?您是否无法在需要进行此更改的主机上运行 PHP?序列化数据在 PHP 中将更容易处理,即使您制作了一个小的 shell 可执行 PHP 脚本来处理它。
[ghoti@pc ~]$ cat indexrepl
#!/usr/bin/env php
<?php
// Usage: indexrepl index newcontent [string]
if ($argc < 4) {
$s='a:3:{s:7:"string1";s:4:"test";s:3:"url";s:16:"http://myurl.com";s:7:"string2";s:4:"test";}';
} else {
$s=$argv[3];
}
$a=unserialize($s);
$a[$argv[1]]=$argv[2];
print serialize($a) . "\n";
[ghoti@pc ~]$
[ghoti@pc ~]$
[ghoti@pc ~]$ ./indexrepl url http://example.com/
a:3:{s:7:"string1";s:4:"test";s:3:"url";s:19:"http://example.com/";s:7:"string2";s:4:"test";}
[ghoti@pc ~]$
[ghoti@pc ~]$ s='a:3:{s:7:"string1";s:4:"test";s:3:"url";s:19:"http://example.com/";s:7:"string2";s:4:"test";}'
[ghoti@pc ~]$ ./indexrepl string1 foo "$s"
a:3:{s:7:"string1";s:3:"foo";s:3:"url";s:19:"http://example.com/";s:7:"string2";s:4:"test";}
更新:根据评论将其包装在外壳结构中:
[ghoti@pc ~]$ cat strings.txt
a:1:{s:3:"foo";s:3:"bar";}
a:1:{s:3:"foo";s:3:"baz";}
a:1:{s:3:"foo";s:5:"snert";}
[ghoti@pc ~]$ while read line; do ./indexrepl foo test "$line"; done < strings.txt
a:1:{s:3:"foo";s:4:"test";}
a:1:{s:3:"foo";s:4:"test";}
a:1:{s:3:"foo";s:4:"test";}
[ghoti@pc ~]$
关于linux - bash 中的 PHP preg_replace,具体情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11321438/
我是 Robert,我在使用 JavaScript 时遇到了一些问题。 我得到了一个 (这是隐藏的)。我唯一想问你的是:我想检查日期是否在 中已通过。如果通过了我想改变CSS中容器的背景颜色。不幸的
所以我的问题是我想要求输入使用扫描仪的信息,但它根本不打印出来。当它显示跳过的扫描仪的值时,Scanner CheeseType = new Scanner(System.in);,我得到 null。
Fe_Order_Items fe_order_items_id fe_order_specification_id fe_users_id fe_menu_items_id fe_order_ite
人们普遍提到 - “Celery 是一个基于分布式消息传递的异步任务队列/作业队列”。虽然我知道如何使用 Celery 工作人员等。但内心深处我不明白分布式消息传递的真正重要性和意义以及任务队列在其中
我试图理解下面的代码,但有一些我以前从未见过的东西,那就是:“\&\&” 这是代码: int main() { fork() \&\& (fork() || fork()); exit(EXIT_SU
您好,我是论坛新手。 我有很多使用 python 的经验,但没有使用 tkinter 的经验。 这是我的代码: from tkinter import * def Done(): celEn
在 C# 中,假设我们有一个通用类和一个具体类 [Serializable] public class GenericUser { ... [Serializable] public class Co
我尝试使用的库有一个通用抽象类,其中有两个实现该基础的子类。我想编写一个类,它将根据构造函数参数的参数类型自动创建其中一个子级的实例。 基类没有默认构造函数 基类的构造函数也需要其他通用类的实例 代码
我是 Angular 的新手,我一直在尝试了解它的工作原理。我正在制作一个简单的应用程序,其中有人可以通过简单的 html 界面添加用户并使用 SQLite 将其存储在数据库中,然后他们可以编辑或删除
我想创建一个用于存储数据的对象,限制读/写访问。 例如: OBJ obj1; OBJ obj2; // DataOBJ has 2 methods : read() and write() DataO
注入(inject)/隔离密封在 dll 中且不实现接口(interface)的类的首选方法是什么? 我们使用 Ninject。 假设我们有一个类“Server”,我们想要注入(inject)/隔离“
在花费了至少 10 个小时的时间浏览在线资源、视频和教程之后,我有两个关于将我的 Android 应用程序与 mySQL 数据库连接的问题。 保存文件 1) 所有教程都将 php 文件保存在 C/WA
许多有经验的开发人员建议不要使用 Django multi-table inheritance因为它的性能不佳: Django gotcha: concrete inheritance通过 Jacob
我知道我冒着挨揍的风险,但我觉得我在这件事上要绕圈子。为了让模型可用于多个项目,我们已将模型移出到一个单独的项目(一个 DLL)中,作为一系列要实现的接口(interface)。我们的界面上有这一行:
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我遇到了一个特定 mac 的问题,它没有显示我正确构建的某个网站。我测试过的所有其他 mac 和 pc 都能正确显示网站,但是在所有浏览器中这个特定的 mac 显示不正确就像提到的那样,这在其他每台计
给定这段代码 public override void Serialize(BaseContentObject obj) { string file = ObjectDataStoreFold
我已经搜索了网络和我的服务器,但我无法找到我网站的 php.ini。我的网站出现以下错误。 Class 'finfo' not found Details G:\inetpub\wwwroot\lan
SQL 爱好者: 我正在尝试通过玩以下用例来挖掘我一些生疏的 sql 技能: 假设我们有一家有线电视公司,并且有跟踪的数据库表: 电视节目, 观看我们节目的客户,以及 观看事件(特定客户观看特定节目的
我正在设计一个使用 HTML5 网络组件(HTML 导入、影子 DOM、模板和自定义 HTML 元素)的网络应用程序,这些组件是通过普通 JavaScript(无框架)实现的。 Web 应用程序相当简
我是一名优秀的程序员,十分优秀!