php - 是否可以在 php 中使用正则表达式替换短语后的单词?
问题描述
输入文字:工学院、医学院
所需输出:教育学校,教育学校
规则:后面跟“school of”的任何词都需要用“education”代替
$inputext = "school of engineering, school of medicine";
$rule ="//s/";
$replacetext = "education";
$outputext = preg_replace($rule, $replacetext, $inputext);
echo($outputext);
感谢您的任何建议。
解决方案
无需捕获任何内容或使用环视。
$inputext = "school of engineering, school of medicine";
$rule ="/school of \K[a-z]+/";
$replacetext = "education";
$outputext = preg_replace($rule, $replacetext, $inputext);
echo $outputext;
匹配前面的单词(和后面的空格of
),用 重新开始全字符串匹配\K
,然后替换目标单词。
推荐阅读
- ubuntu - 我在并行积分计算程序中没有看到任何错误,但出现分段错误(代码 139),我该如何解决?
- angular - 扩展接口的可选泛型
- javascript - 通过 URL 传递参数将其显示为 [object object] 而不是字符串(打字稿)
- python - 修复损坏的 Anaconda 环境
- javascript - 如何从电子表格中获取数据并将其发送到 html 表格 googel script web
- python - 如果循环熊猫数据框字典
- c++ - QPushButton 在第二个布局中不可点击
- c# - 从阅读窗格中获取单击的超链接
- java - DBCC CHECKDB 未在 Java (MS SQL) 中返回结果集
- visual-studio - Visual Studio 2019 和经典 ASP:无法打开脚本文档