Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);

?>
share|improve this question
    
I didn't know that Dr Phil was a PHP guru? –  Krister Andersson Dec 29 '11 at 18:07
    
$element_pos[(string)$html_element]???? is null??? –  Olaf Erlandsen Dec 29 '11 at 18:13
    
Dr. Phil what? lol –  NetSkay Dec 29 '11 at 18:57
    
Might want to check out this question too: stackoverflow.com/questions/3654681/… –  RobW Aug 2 '12 at 19:26

1 Answer 1

up vote 1 down vote accepted

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

foreach($definitions as $html_element => $value){
share|improve this answer
    
so the way i have it i am just accessing the value of the array? –  NetSkay Dec 29 '11 at 19:15
    
@NetSkay - Jupp, that's correct. –  Krister Andersson Dec 29 '11 at 19:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.