简体   繁体   中英

PHP array key is NULL, array appears empty

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

?>

如果这是要用作$html_element作为索引的键,则应该执行以下操作:

foreach($definitions as $html_element => $value){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM