-4

I have a code that searches tags in .html file but I have problem executing the script it leads me to undefined index.

on my previous QUESTION I ask about searching id tags and I't leads me to used it as a reference. Enhancing the code and executing the code correctly but it shows me an error. The error searches every id tags in a .html file

CODE:

<?php
function getElementById($matches)
{
    global $data;
    return $matches[1].$matches[3].$matches[4].$data[$matches[3]].$matches[6];
}

$data['test'] = 'A';

$filename = 'test.html';

$html = file_exists($filename) ? file_get_contents($filename) : die('can\'t open the file');

$_HTML = preg_replace_callback('#(<([a-zA-Z]+)[^>]*id=")(.*?)("[^>]*>)([^<]*?)(</\\2>)#ism', 'getElementById', $html);

echo $_HTML;
?>

HTML:

<html>
    <head>
        <title>TEST</title>
    </head>
    <body>
        <div id="test"></div>
        <div id="test2"></div>
    </body>
</html>

OUTPUT: PRINTSCREEN

Community
  • 1
  • 1
Detention
  • 167
  • 1
  • 1
  • 10
  • 4
    [You can't parse HTML with regular expressions](http://stackoverflow.com/a/1732454/156811). – Havenard Nov 15 '13 at 23:49
  • 2
    Seems the code *that was written for you* is working ok. You just didn't define `test2` in your `$data` array. – mario Nov 15 '13 at 23:50
  • I don't get it, do you not understand the error message? Seems pretty clear, but I don't understand why you are doing this. If you actually want to do this (and you probably don't, see the other comments/answers), you could _at least_ use `isset()` – Matt Nov 15 '13 at 23:53

4 Answers4

2

Here's how you can implement a default:

$data3 = isset($data[$matches[3]]) ? $data[$matches[3]] : 'default';
return $matches[1].$matches[3].$matches[4].$data3.$matches[6];
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Disclaimer: You shouldn't be doing all this regex stuff with HTML, blah, blah...

But if you insist

function getElementById($matches)
{
    global $data;
    return $matches[1]
        .$matches[3]
        .$matches[4]
        .isset($data[$matches[3]]) ? $data[$matches[3]] : 'DEFAULT_VALUE'
        .$matches[6];
}

Why not use regex?

https://stackoverflow.com/a/1732454/156811

Using regular expressions to parse HTML: why not?

http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html

I'm sure you can find more if you do a quick search

Some alternatives:

http://us1.php.net/dom

http://simplehtmldom.sourceforge.net/

etc.

Community
  • 1
  • 1
Matt
  • 5,478
  • 9
  • 56
  • 95
  • whay there's a disadvantage using regex on html? – Detention Nov 16 '13 at 00:07
  • Have you even been reading the responses here? The first link in the comments on your question is probably a good starting point. HTML is context free, not regular, so it can't be done in a general case. For a specific small case (e.g. you have **well known** HTML that you create yourself that will always follow certain rules and you need to get something small out of it), then maybe regex makes some sense. – Matt Nov 16 '13 at 00:12
0

Regex is not really the correct way todo this. I suggeset you using XPATH or anything like that. You can also use something like this:

https://code.google.com/p/phpquery/

Dirkos
  • 488
  • 1
  • 10
  • 33
0

I don't understand what is your problem?

It's 100% normal that you got an undefined index error.

In your HTML, 2 IDs are defined: 'test' and 'test2'.

Your PHP code finds those two ID and look for an entry inside $data, however, $data contains only an entry for 'test', so PHP tells you that there is no entry for 'test2': Notice: Undefined index: test2

That's it :-)

user2998227
  • 81
  • 1
  • 2