2
<div style="display:none">250</div>.<div style="display:none">145</div>

id want:

<div style="display:none">250</div>#.#<div style="display:none">145</div>

or like this:

<div style="display:none">111</div>125<div style="display:none">110</div>

where id want

<div style="display:none">111</div>#125#<div style="display:none">110</div>

id like a preg replace to put those hashtags around the numb, so i asume the REGEX would look something like this:

"<\/div>[.]|<\/div>\d{1,3}"

The digit (in case its a digit, can be 1-3 digits), or it can be a dot.

Anyhow, i dont know hot to preg replace around the value:

"<\/div>[.]|<\/div>\d{1,3}" replace: $0#

Inserts it after the value..

EDIT

I cannot use a HTML parser, because i cannot find one that does not threat styles / classes as plaintext, and i need the values attached, to determine if the element is visible or not :(

and yes, it is driving me insane, but i am almost done :)

  • 3
    Please refrain from parsing HTML with RegEx as it will [drive you į̷̷͚̤̤̖̱̦͍͗̒̈̅̄̎n̨͖͓̹͍͎͔͈̝̲͐ͪ͛̃̄͛ṣ̷̵̞̦ͤ̅̉̋ͪ͑͛ͥ͜a̷̘͖̮͔͎͛̇̏̒͆̆͘n͇͔̤̼͙̩͖̭ͤ͋̉͌͟eͥ͒͆ͧͨ̽͞҉̹͍̳̻͢](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an [HTML parser](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) instead. – Madara's Ghost Dec 01 '13 at 20:51

2 Answers2

2

You really should not be trying to parse HTML with regex. There are only a couple of people I know who can do it. And even if you would have been one of them regex still is not the right tool for the job. Use PHP's DOMDocument optionally with DOMXPath.

With xpath:

$dom = new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

$textNode = $xpath->query('//text()')->item(1);

$textNode->parentNode->replaceChild($dom->createTextNode('#' . $textNode->textContent . '#'), $textNode);

echo htmlspecialchars($dom->saveHTML());

http://codepad.viper-7.com/KLTLDA

With childnodes:

$dom = new DOMDocument();
$dom->loadHTML($html);

$body = $dom->getElementsByTagName('body')->item(0);
$textNode = $body->childNodes->item(1);

$textNode->parentNode->replaceChild($dom->createTextNode('#' . $textNode->textContent . '#'), $textNode);

echo htmlspecialchars($dom->saveHTML());

http://codepad.viper-7.com/Ii4vPb

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
0

In your case,

preg_replace("~</div\s*>(\.|\d{1,3})<div~i", '</div>#$1#<div', $string);

That's assuming no spaces between the divs and the content, and nothing otherwise weird is between.

Note that regex is very brittle, and would fail silently on even the slightest change in HTML.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308