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 替换两个字符串之间的字符串
基础教程推荐
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- php中的foreach复选框POST 2021-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- 将变量从树枝传递给 js 2022-01-01
- php中的PDF导出 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
