用正则表达式替换PHP中的一些字符串?

编程入门 行业动态 更新时间:2024-10-12 12:33:17
本文介绍了用正则表达式替换PHP中的一些字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要做一个简单的正则表达式查找,并用PHP替换我的PHP语法突出显示脚本.

I need to do a simple regex find and replace with PHP for my syntax highlighting script in PHP.

我需要采用一串代码,实际上这将是一个完整的php文件,并被读取为这样的字符串.

I need to take a string of code which will actually be a whole php file that is read into a string like this.

$code_string = 'the whole source code of a php file will be held in this string';

然后找到所有出现的情况并进行替换...

And then find all occurences of these and do a replace...

查找: [php] 并替换为 <pre class="brush: php;"> 查找: [/php] 并替换为 </pre>

Find: [php] and replace with <pre class="brush: php;"> Find: [/php] and replace with </pre>

找到 [javascript] 并替换为 <pre class="brush: js;"> 查找: [/javascript] 并替换为 </pre>

Find [javascript] and replace with <pre class="brush: js;"> Find: [/javascript] and replace with </pre>

我真的对正则表达式不好,有人可以帮我吗?

I really am not good with regex could someone please help me with this?

推荐答案

要替换字符串中的字符串,您只需str_replace();.如果我正确理解了您的问题,它将看起来像这样:

For the replacement of strings within strings you can just str_replace();. If I understand your question correctly it would look something like this:

$code_string = htmlentities(file_get_contents("file.php")); $old = array("[php]","[javascript]","[/php]","[/javascript]"); $new = array('<pre class="brush: php;">','<pre class="brush: js;">','</pre>','</pre>'); $new_code_string = str_replace($old,$new,$code_string); echo $new_code_string;

更多推荐

用正则表达式替换PHP中的一些字符串?

本文发布于:2023-11-30 07:05:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649113.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   正则表达式   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!