0

I'm rather new to PHP and I am kind of stuck here writing this simple script; what I am trying to ultimately do is go through the content of a string and find the positions of all the occurrences I have listed in my $definitions array then map those positions in a separate array and return it... rather simple but I am not sure where the problem arises, when i print_r on the array in different parts of code, thinking its a scope issue, I keep seeing that the key value of the array is NULL and also when I try and access a value of the array I am sure exists for a given key, i also get nothing; any help would be appreciated...

thank you!

<?php

class html2DokuWiki {
    function definition_map($content){

        $definitions = array("<title" => " ","<h" => array("=", 6),"<p" => "\n\n","<b" => "**","<strong" => "**","<em" => "//","<u" => "__","<img" => " ","<a" => " ","<ul" => " ","<ol" => "*","<li" => "-","<dl" => " ","<dt" => " ","<dd" => " ");

        $element_pos = array();
        foreach($definitions as $html_element){
            $offset = 0;
            $counter = 0;
            $element_pos[(string)$html_element] = array(); //ask phil why do i need to cast in order to use the object?
            while($offset = strpos($content, $html_element, $offset + 1)){
                $element_pos[(string)$html_element][] = $offset;
            };
        };
        //print_r($element_pos);
        echo $element_pos["<p"][0];
        return $element_pos;}

    function run($page){
        return $this->definition_map($page);}
};

$debug = new html2DokuWiki();
$url = "http://www.unixwiz.net/techtips/sql-injection.html";
$content = file_get_contents($url);
//echo $content;
//print_r($debug->run($content));
$test = $debug->run($content);
echo "<p> THIS:".$test["<p"][0]."</p>";
//print_r($test);

?>
3
  • I didn't know that Dr Phil was a PHP guru? Commented Dec 29, 2011 at 18:07
  • $element_pos[(string)$html_element]???? is null??? Commented Dec 29, 2011 at 18:13
  • Might want to check out this question too: stackoverflow.com/questions/3654681/… Commented Aug 2, 2012 at 19:26

1 Answer 1

1

If it's the key you want to use as $html_element as an index you should do:

foreach($definitions as $html_element => $value){
Sign up to request clarification or add additional context in comments.

1 Comment

so the way i have it i am just accessing the value of the array?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.