I
I
isq2017-10-11 19:15:02
PHP
isq, 2017-10-11 19:15:02

Why are results repeated when parsing in PHP?

Function Code

spoiler
<?php

function poisk($x = null, $index = null){

  $content = zapros('тут url');

  preg_match_all('/<div class="post_line lin(.*?)\>/s', $content, $str);

  $y = 1;

  
  while($y <= $x){

    preg_match('/data-type="(.*?)\_/', $str[1][$y], $type);
    
    if($index == 1){

      switch($type[1]){

        case 'posts':{

          preg_match('/line post(.*?)\"/', $str[1][$y], $id);
          $result = $id[1];
          break;

        }
      }

    $y++;

    }else{

      switch($type[1]){

        case 'posts':{
            
          preg_match('/line post(.*?)\"/', $str[1][$y], $id);
          $result = $id[1];
          break;

        }

        case 'blogs':{
            
          preg_match('/line post(.*?)\"/', $str[1][$y], $id);
          $result = $id[1];
          break;

        }
      }	

    $y++;
      
    }

  echo $id[1].'   '.$result.'<br />';

  }
}

poisk(4, 1);
?>

The problem is the following when calling the function poisk(4, 1 ); with parameter 1 , 4 lines should be displayed that satisfy the requirement of the type posts , with parameter 0 , everything in a row. But with a value of 0, it happens as expected, and with 1 , they either repeat or first an empty line comes first, then repeating lines. What's wrong? I've already broken my head.
Here is an example that I parsed
spoiler
<div style="display: none">
      <div class="post_line line post405462" data-type="blogs_type">
        <div class="w60 left align-center">
          405462				</div>
        <div class="w300 left">
          <span class="project_name">Новости</span>
        </div>
        <div class="w150 left">
          <a class="button projectDetails" data-id="405462"  data-href="" href="javascript:void(0);">Посмотреть новость</a>
        </div>
        <div class="w150 right align-center">
          <a href="javascript:" id="405462" title="Оставить отзыв" class="tipsy_class bother_project"></a>
          <a href="javascript:" data-id="405462" title="Поставить оценку" class="tipsy_class delete_project"></a>
        </div>
        <div class="clear"></div><!-- /clear -->
      </div>
      </div>


      <div style="display: none">
      <div class="post_line line post405463" data-type="posts_type">
        <div class="w60 left align-center">
          405463				</div>
        <div class="w300 left">
          <span class="project_name">Новости</span>
        </div>
        <div class="w150 left">
          <a class="button projectDetails" data-id="405463"  data-href="" href="javascript:void(0);">Посмотреть новость</a>
        </div>
        <div class="w150 right align-center">
          <a href="javascript:" id="405463" title="Оставить отзыв" class="tipsy_class bother_project"></a>
          <a href="javascript:" data-id="405463" title="Поставить оценку" class="tipsy_class delete_project"></a>
        </div>
        <div class="clear"></div><!-- /clear -->
      </div>
      </div>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question