php preg_replace regex replace string between two string(php preg_replace regex 替换两个字符串之间的字符串)
问题描述
我有以下问题:我想替换(在 php 中)一个特殊字符,但前提是它位于其他两个字符之间.它试图用 preg_replace 找到解决方案,但没有奏效.
I have the following problem: I want to replace (in php) a special character, but only if it's between two other characters. It tried to find a solution with with preg_replace but it doesn't work.
我想替换每一个;带有 : 介于 "示例:
I want to replace every ; with a : which is between the " The Examples:
$orig_string= 'asbas;"asd;";asd;asdadasd;"asd;adsas"'
结果应该是:
'asbas;"asd:";asd;asdadasd;"asd:adsas"'
我尝试了几个正则表达式,但没有任何成功......
I tried several regexes but without any succes...
我试过的两个例子:
$result = preg_replace('(?<=")(.*)(;)(.*)(?=")',':', $str);
$result = preg_replace('.*".*(;).*"',':', $str);
有人可以帮我吗?
非常感谢
V
推荐答案
在这里你不需要使用环顾四周.可以写成
You need not use look arounds here. It can be written as
("[^";]*);([^"]*")
替换为 1:2
正则表达式演示
测试
preg_replace ("/("[^";]*);([^"]*")/m", "\1:\2", 'asbas;"asd;";asd;asdadasd;"asd;adsas"' );
=> asbas;"asd:";asd;asdadasd;"asd:adsas"
更新:
;(?!(?:"[^"]*"|[^"])*$)
只需将匹配的 ;
替换为 :
Just replace the matched ;
with :
演示
这篇关于php preg_replace regex 替换两个字符串之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:php preg_replace regex 替换两个字符串之间的字符串


基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01