1

i wanna a regex to capture any thing between <h1> and <br /> into matches['name']. and any thing between <h1 style="float: left;"> and </h2> to matches['cost']. how i should do it ?

Cheers,

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
sataho
  • 555
  • 1
  • 4
  • 4
  • 5
    Do not use regular expressions for this. Use a proper HTML parser. Also, your request doesn't seem to make much sense - do you really want the data between these non-matching tags? – Pekka Aug 07 '11 at 16:32
  • possible duplicate of [Best methods to parse HTML with PHP](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html-with-php) – Pekka Aug 07 '11 at 16:33
  • @Pekka For particular purposes regexps are easier way to go. – Karolis Aug 07 '11 at 16:34
  • yes i want this ! and want regex too ! – sataho Aug 07 '11 at 16:36
  • How many non-matching tag did you expect to find? Is this a common feature on the page you're parsing? – Damien Pirsy Aug 07 '11 at 17:13
  • The
    cannot hold! http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
    – tobyodavies Aug 08 '11 at 06:00

2 Answers2

1

This is not an HTML parser, it's just a regex based string search (Demo):

$searches = array(
    'name' => '<h1>(.*)<br />',
    'cost' => '<h1 style="float: left;">(.*)</h2>'
);

$matches = array();
foreach($searches as $name => $pattern)
{
    $r = preg_match_all("~{$pattern}~", $str, $matches[$name]);
    $matches[$name] = $matches[$name][1];
}

print_r($matches);

Output:

Array
(
    [name] => Array
        (
            [0] =>  name1
            [1] =>  name2
        )

    [cost] => Array
        (
            [0] =>  cost1
            [1] =>  cost1
        )

)
hakre
  • 193,403
  • 52
  • 435
  • 836
0
preg_match('/<h1 style="float: left;">(?P<cost>.*?)<\/h1>.*<h1>(?P<name>.*?)<br \/>/s', $string, $matches);

echo $matches['name'];
echo $matches['cost'];
Karolis
  • 9,396
  • 29
  • 38
  • thanks I want some thing same to Your answer ! but in doesent work when i teted with this : $string='

    xxxxxxxxxxx

    rrrrrrrrrrrrrrrr
    zzzzzzzzzz

    ' ;
    – sataho Aug 07 '11 at 17:10
  • xxxxxxxxxxx

    rrrrrrrrrrrrrrrr
    zzzzzzzzzz

    ' ; preg_match('/

    (?P.*?)

    .*?

    (?P.*?)
    /s', $data, $matches); echo $matches['name']; echo $matches['cost']; ?> dont show any thing yet !!! :((

    – sataho Aug 07 '11 at 17:29
  • @sataho You made a bad copy of my script :) It seems that you removed some backslashes. – Karolis Aug 07 '11 at 17:34
  • thanks a lot. can you send me a .php version of this program that works correct ? littlejardin@yahoo.com – sataho Aug 07 '11 at 17:41
  • 1
    @sataho The code in my answer is a working code. Full version: `

    xxxxxxxxxxx

    rrrrrrrrrrrrrrrr
    zzzzzzzzzz

    ' ; preg_match('/

    (?P.*?)<\/h1>.*

    (?P.*?)
    /s', $data, $matches); echo $matches['name']; echo $matches['cost']; ?>`

    – Karolis Aug 07 '11 at 17:56