0

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>
Andy Lester
  • 91,102
  • 13
  • 100
  • 152
kiki33
  • 3
  • 2
  • this one is way out of context... Explain your requirements well – Huzaib Shafi Dec 14 '15 at 17:22
  • 1
    You need to use the `s` modifier so the `.` extends to newlines. Your "multiple lines html" are all on one line here so maybe that isn't the issue? You're more than likely better off using a parser. – chris85 Dec 14 '15 at 17:23
  • 1
    Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – chris85 Dec 14 '15 at 17:26
  • Use an HTML parser. The problem you are having is the #1 reason that processing HTML with regexes is a road to frustration. – Andy Lester Dec 14 '15 at 17:37
  • Okey, i need example. – kiki33 Dec 14 '15 at 17:41
  • You might want to try this link from another stackoverflow question: http://stackoverflow.com/questions/2240348/php-preg-replace-regex-that-matches-multiple-lines –  Dec 14 '15 at 17:25
  • There are numerous parsers and examples on the linked thread. If you have a specific issue with a parser please update your question with the code and the issue. – chris85 Dec 14 '15 at 17:42
  • But I do not understand. – kiki33 Dec 14 '15 at 17:43

1 Answers1

0

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>\n"; 

preg_match_all($re, $str, $matches);
Community
  • 1
  • 1
Huzaib Shafi
  • 1,153
  • 9
  • 24