如何在php中使用preg_replace_callback()来替换文本模式? [英] How to use preg_replace_callback() in php to replacing text pattern?

查看:93
本文介绍了如何在php中使用preg_replace_callback()来替换文本模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计们,

我的字符串就像
[#ff0000] RedColor
[#0000ff] BlueColor [/#0000ff]
[/#ff0000]
我希望获得HTML代码,例如
< font color =#ff0000> RedColor
< font color = #0000ff> BlueColor< / font>
< / font>
来自该字符串









以下php代码是完美的工作,只有一个字体标记

  $ text  = '  [#ff0000] RedColor [/#ff0000]';  //  <  - 仅用一个标记替换的文本 
$ text = preg_replace_callback(' / \ [(#[AF,af,0 -9] {6})\](。*?)\ [(\ /#[AF,af,0-9] {6})\] /',功能($ m ){
if( /。$ m [1] == $ m [ 3])
return ' < font color ='。$ m [1]。' >。$ m [2]。' < / font>';
},$ text) ; // < - 这是返回html代码的函数
echo $ text ;





Out Put

RedColor



但是当使用嵌套字体(模式)意外结果时我得到了

  $ text  = '  [#ff0000] RedColor 
[#0000ff] BlueColor [/#0000ff]
[/#ff0000]'
; // < - 用嵌套标签替换的文本
$ text = preg_replace_callback(' / \ [(#[AF,af,0- 9] {6})\](。*?)\ [(\ /#[AF,af,0-9] {6})\] /',功能($ m) {
if( /。$ m [1] == $ m [3 ])
return ' < font color =''。$ m [1]。' >。$ m [2]。' < / font>';
},$ text); // < - 这是返回html代码的函数
echo $ text ;



我得到如下输出

[/#ff0000]

而不是

RedColor BlueColor




i访问了以下链接:这里 [ ^ ]但我没有解决方案

thanx。

解决方案

text = ' [#ff0000] RedColor [/#ff0000]'; // < - 仅用一个标记替换的文本


text = preg_replace_callback(' / \\ \\ [(#[AF,AF,0-9] {6})\(。*?)\ [(\ /#[AF,AF,0-9] {6})\] / ',function(


m){
if( /

hi guys,

i have string like "
 [#ff0000] RedColor
      [#0000ff]BlueColor[/#0000ff]
 [/#ff0000]"
and i want to get the HTML code like "
 <font color="#ff0000"> RedColor
      <font color="#0000ff">BlueColor</font>
 </font>"
 from that string





following php code is perfect work with only one font tag

 $text = '[#ff0000] RedColor [/#ff0000]'; // <- text to replace with only one tag
$text = preg_replace_callback('/\[(#[A-F,a-f,0-9]{6})\](.*?)\[(\/#[A-F,a-f,0-9]{6})\]/', function ($m) {
          if("/".$m[1]==$m[3])
         return '<font color="'.$m[1].'">'.$m[2].'</font>';
}, $text);  // <- this is function that return the html code
echo $text;



Out Put
RedColor

but when using nested font(pattern) unexpected result i get

 $text = '[#ff0000] RedColor
      [#0000ff]BlueColor[/#0000ff]
[/#ff0000]'; // <- text to replace with nested tags
 $text = preg_replace_callback('/\[(#[A-F,a-f,0-9]{6})\](.*?)\[(\/#[A-F,a-f,0-9]{6})\]/', function ($m) {
           if("/".$m[1]==$m[3])
          return '<font color="'.$m[1].'">'.$m[2].'</font>';
 }, $text);  // <- this is function that return the html code
 echo $text;


And i got the output like below
[/#ff0000]
instead
RedColor BlueColor



i have visited the following link : here[^] but no solution i got
thanx.

解决方案

text = '[#ff0000] RedColor [/#ff0000]'; // <- text to replace with only one tag


text = preg_replace_callback('/\[(#[A-F,a-f,0-9]{6})\](.*?)\[(\/#[A-F,a-f,0-9]{6})\]/', function (


m) { if("/".


这篇关于如何在php中使用preg_replace_callback()来替换文本模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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