Explode in PHP returns Undefined offset: 1

Asked

Viewed 6,034 times

5

function extrai($string) {
    $peca1 = explode('id="', $string);
    $peca2 = explode('">', $peca1[1]);

    return $peca2[0];
}

To string that’s the one:

< ul class="artigo" id="artigo1">
 < li>
   < p>Art. 1º  Salvo disposição contrária, a lei começa a vigorar em todo o país quarenta e cinco dias depois de oficialmente publicada.< /p>
  < /li>
< /ul>

Reports the error Undefined offset: 1 on the line of peca2 function. Someone knows the reason?

1 answer

5


You are trying to access a value whose index is not available on array. The example you posted is functional:

function extrai($string) {
    $peca1 = explode('id="', $string);
    $peca2 = explode('">', $peca1[1]);
    return $peca2[0];
}

$string = '< ul class="artigo" id="artigo1">
 < li>
 < p>Art. 1º  Salvo disposição contrária, a lei começa a vigorar em todo o país quarenta e cinco dias depois de oficialmente publicada.< /p>
 < /li>
 < /ul>';

echo extrai($string); // artigo1

DEMO

Another way using the function preg_match:

function obterID($string) {
    $res = preg_match('~class="artigo" id="([\w]+)"~i', $string, $IDs);
    if ($res)
        return $IDs[1];
    else
        return "";  
}

And to use in your code:

// $documento = file_get_contents($arquivo);
$linhasExpl = explode("<ul", $documento);
$linhas = "";
$postId = 100;

$titulo = "TÍTULO I";
$categoria = "constituicao-da-republica-federativa-do-brasil-de-1988";
$tituloCategoria = "CONSTITUIÇÃO DA REPÚBLICA FEDERATIVA DO BRASIL DE 1988";

foreach ($linhasExpl as $linha){
    $data = date(DATE_RFC822);
    $data2 = date("Y-m-d h:i:s");
    $tituloSlug = ObterID($linha);
    if (empty($tituloSlug))
        continue;

    $linhas .= "<item>" . "\r\n";
    $linhas .= "<title>{$titulo}</title>" . "\r\n";
    $linhas .= "<link>http://localhost/votanalei/{$tituloSlug}</link>" . "\r\n";
    $linhas .= "<pubDate>{$data}</pubDate>" . "\r\n";
    $linhas .= "<dc:creator><![CDATA[ale]]></dc:creator>" . "\r\n";
    $linhas .= "<guid isPermaLink='false'>http://localhost/votanalei/?p={$postId}</guid>" . "\r\n";
    $linhas .= "<description></description>" . "\r\n";
    $linhas .= "<content:encoded><![CDATA[<ul{$linha}]]></content:encoded>" . "\r\n"; 
    $linhas .= "<excerpt:encoded><![CDATA[]]></excerpt:encoded>" . "\r\n";
    $linhas .= "<wp:post_id>{$postId}</wp:post_id>" . "\r\n";
    $linhas .= "<wp:post_date>{$data2}</wp:post_date>" . "\r\n";
    $linhas .= "<wp:post_date_gmt>{$data2}</wp:post_date_gmt>" . "\r\n";
    $linhas .= "<wp:comment_status>open</wp:comment_status>" . "\r\n";
    $linhas .= "<wp:ping_status>open</wp:ping_status>" . "\r\n";
    $linhas .= "<wp:post_name>{$tituloSlug}</wp:post_name>" . "\r\n";
    $linhas .= "<wp:status>publish</wp:status>" . "\r\n";
    $linhas .= "<wp:post_parent>0</wp:post_parent>" . "\r\n";
    $linhas .= "<wp:menu_order>0</wp:menu_order>" . "\r\n";
    $linhas .= "<wp:post_type>post</wp:post_type>" . "\r\n";
    $linhas .= "<wp:post_password></wp:post_password>" . "\r\n";
    $linhas .= "<wp:is_sticky>0</wp:is_sticky>" . "\r\n";
    $linhas .= "<category domain='category' nicename='{$categoria}'><![CDATA[{$tituloCategoria}]]></category>" . "\r\n";
    $linhas .= "<category domain='post_tag' nicename='{$categoria}'><![CDATA[{$tituloCategoria}]]></category>" . "\r\n";
    $linhas .= "<wp:postmeta>" . "\r\n";
    $linhas .= "<wp:meta_key>_edit_last</wp:meta_key>" . "\r\n";
    $linhas .= "<wp:meta_value><![CDATA[1]]></wp:meta_value>" . "\r\n";
    $linhas .= "</wp:postmeta>" . "\r\n";
    $linhas .= "</item>" . "\r\n";
    $postId += 5;
}

$file = fopen("test.txt", "w");
$results = fwrite($file, $linhas);
fclose($file);

DEMO

  • If you’re right, why does this Undefined error offset: 1

  • @Stories Why are you trying to access a value that is not available at that position of array. Take the example: http://ideone.com/e6NEpE

  • Right, the value is not available at this position, but as I can access or correct the error

  • does not show error, but in echo sai class="article"

  • I want to extract the id of ul. And now I invert the function Function extract_unit($string) { $peca1 = explode('">', $string); $peca2 = explode('id="', $peca1[0]); Return $peca2[1]; } Now print the articles with echo, but it shows the Undefined offset error: 1, in the row Return $peca2[1];

  • I’m trying to use, but, I don’t know how to implement in my code, which is at this link http://pastebin.com/vG6BSpuu. I don’t know how to use foreach, inside another foreach, to generate only one id per array

  • No echo prints, but line 69 and 81 does not print

  • 1

    @There must be that other problem of yours, of the other question, I tried to tidy up the code a little, see: http://pastebin.com/EYsVXyV1, has a line that I left commented by not knowing how it should look, but just you arrange. =)

  • Your genius!!!!!!!! kkkk It worked, man. showwww. But you know what the problem was?

  • @Alêmoraes kkk, I think the empty helped a lot. Well, if possible mark the answer as accepted. =)

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.