PHP 使用 preg_replace 用双引号替换单词 [英] PHP Use preg_replace for replacing word with Double quotes

查看:56
本文介绍了PHP 使用 preg_replace 用双引号替换单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
preg_replace 如何围绕字符串的 html 属性与在 PHP 中

如何使用 preg_replace() 更改 < >= 之后的所有单词> 用双引号括起来的单词

How to use preg_replace() for change all word that is within < > and after = for word with double quote surround

$var="<myfootball figure=thin new=aux(comment); > this=Association football < name=football >"

$var="<myfootball figure="thin"  new="aux(comment);" >this=Association footballl<  name="football"  >"

使用 preg_replace() 执行此操作的正则表达式是什么?

What is the regular expresion for do this with preg_replace()?

推荐答案

Replace (?<==)(\b\w+\b)(?!")(?=[^<]*>)"$1"

Replace (?<==)(\b\w+\b)(?!")(?=[^<]*>) with "$1"

$var = preg_replace('/(?<==)(\b\w+\b)(?!")(?=[^<]*>)/', '"$1"', $var);

<小时>

编辑(基于 OP 的评论和问题更新)>>


EDIT (based on OP's comment and question update) >>

替换(?<==)(\b\S+?)(?=[\s>])(?!")(?=[^<]*>)"$1"

$var = preg_replace('/(?<==)(\b\S+?)(?=[\s>])(?!")(?=[^<]*>)/', '"$1"', $var);

这篇关于PHP 使用 preg_replace 用双引号替换单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆