Take string size inside array with PHP

Asked

Viewed 417 times

1

I have the following problem, in the program below, I took the forms of a site, but I have to specify the exact length of the line that has the form(96), I would have the possibility to take this number without having to specify manually.

<?php
         $url = file_get_contents('site.com');
         if(!$url) {
           trigger_error('Não foi possível ler a url', E_USER_NOTICE);
         }

         $var1 = explode("<form", $url);
         $string = array();

         // Pega o formulário do arquivo HTML.
         for($i = 0; $i < 96; $i++) {
            $string[0][$i] = $var1[1][$i];
         }

?>

I tried to use the foreach, but it doesn’t return the right number.

$i = 0;
foreach($var1 as $valor => $detalhes) {
    foreach($detalhes as $detalhes => $saida) {
        $i++;       
     }
}

Grateful!

  • I didn’t understand that 96... would be what?

  • It is the line size of the html form, e.g.: <form method="post" class="form" action=...>.

  • In total 96 characters

  • Try it this way strlen($var1[1])

  • 3

    The size of the array is count( ), and string size has strlen( ) and mb_strlen( )

  • Opa, the strlen function worked! in case it takes up to the last line of the html file, but I solve with an if. Fight!

  • 3

    Get used to a read on PHP manual, good part has the option of Portuguese. If you give a general view, you will get a good idea. I’m not just talking about researching the function you need, I’m talking about a general view to know where to find what you need.

  • Yes, it really helps a lot. I’ll take it as a total reference. Thank you very much!

Show 3 more comments

1 answer

1


In your case it is not take the size of the array, but the index of the array you are selecting.

Like the index [1] array $var1 is a string, you can count its size with the function strlen():

for($i = 0; $i < strlen($var1[1]); $i++) {
   $string[0][$i] = $var1[1][$i];
}

Browser other questions tagged

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