douzhicui2209 2015-12-14 17:19
浏览 6

preg_replace更多行php

The code:

$message = preg_replace("/<div style='background-color:#C0C8D0;width:95%;'>SMA Forr&aacute;sk&oacute;d: <a href='' onclick='selectcode\\((.*)\\);return false;'>\\[ Mindet kijelol \\]<\\/a><\\/div><div id='(.*)' style=\"width:95%;max-width:95%;max-height: 500px; overflow:scroll;background-color: #FFFFFF;\"><pre class=\"sma\" style=\"font-family:monospace;font-size: 12px;\"><ol><li style=\"font-weight: normal; vertical-align:top;\"><div style=\"font: normal normal 1em\\/1\\.2em monospace; margin:0; padding:0; background:none; vertical-align:top;\">(.*)<\\/div><\\/li><\\/ol><\\/pre><\\/div>/", '[sma]<pre>$3</pre>[/sma]',$message);

Its work, only if the post is have only one line. I want to use multiple lines:

Example: Now i edit this post

->

[sma]Now i edit this post[/sma]

Its good, and i have more lines: Example:

line1
line2
line3
line4
line5
line6

this output:

[sma]line1line2line3line4line5line6[/sma]

and i want:

[sma]line1

line2

line3

line4

line5

line6
[/sma]

The multiple lines html output:

<div style='background-color:#C0C8D0;width:95%;'>SMA Forr&aacute;sk&oacute;d: <a href='' onclick='selectcode(93347);return false;'>[ Mindet kijelol ]</a></div><div id='93347' style="width:95%;max-width:95%;max-height: 500px; overflow:scroll;background-color: #FFFFFF;"><pre class="sma" style="font-family:monospace;font-size: 12px;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">line1</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">line2</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">line3</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">line4</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">line5</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">line6</div></li></ol></pre></div>
  • 写回答

1条回答 默认 最新

  • doufu5521 2015-12-14 18:10
    关注

    I believe you are either trying to learn regex parsing in PHP or you are trying to parse HTML to make something out of it. I did it once to make an XML generator named hFeeds (Check the Development Branch for my latest commits). You should have a look at its code, in case you are trying to achieve the same. [Note: I stopped working on it a long time ago, because I developed another better one using Laravel framework and currently empowering my website MonitorKashmir.com namely haaput.

    As the comments above suggested, parsing HTML using regular expressions is hardly recommended. In most of the cases, you should use HTML/XML Parsers, as suggested above, e.g; SimpleXML provided within PHP.

    Some suggestions:

    1. Study about SimpleXML and its usage.
    2. Use Regex101.com to check out regular expressions and its Code Generator to generate PHP code (at-least for time-being)

    Anyways for the problem above, if we analyse, we need a pattern that repeats itself. In this case, it is:

    <div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">[CONTENT TO BE CAPTURED]</div>
    

    So we only need to capture the [CONTENT TO BE CAPTURED] part and we should be fine. As per all the information provided here-in, I assume that the [CONTENT TO BE CAPTURED] is only alpha-numeric, that is, it contains only Letters & digits until next </div> is encountered.

    So the solution for the problem will be the following {$str can contain content from some url, e.g;

    $str = file_get_contents("http://www.example.com/example.html");
    

    And it can be replaced in the following code accordingly }.

    $re = "/<div style=\"font: normal normal 1em\\/1\\.2em monospace; margin:0; padding:0; background:none; vertical-align:top;\">([[:alnum:]]*)<\\/div>/"; 
    $str = "<div style='background-color:#C0C8D0;width:95%;'>SMA Forr&aacute;sk&oacute;d: <a href='' onclick='selectcode(93347);return false;'>[ Mindet kijelol ]</a></div><div id='93347' style=\"width:95%;max-width:95%;max-height: 500px; overflow:scroll;background-color: #FFFFFF;\"><pre class=\"sma\" style=\"font-family:monospace;font-size: 12px;\"><ol><li style=\"font-weight: normal; vertical-align:top;\"><div style=\"font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;\">line1</div></li><li style=\"font-weight: bold; vertical-align:top;\"><div style=\"font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;\">line2</div></li><li style=\"font-weight: normal; vertical-align:top;\"><div style=\"font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;\">line3</div></li><li style=\"font-weight: bold; vertical-align:top;\"><div style=\"font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;\">line4</div></li><li style=\"font-weight: normal; vertical-align:top;\"><div style=\"font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;\">line5</div></li><li style=\"font-weight: bold; vertical-align:top;\"><div style=\"font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;\">line6</div></li></ol></pre></div>
    "; 
    
    preg_match_all($re, $str, $matches);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题