explode with the foreach not working (beginner)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sathyashrayan

    explode with the foreach not working (beginner)


    Dear group,
    The function to be used as follows:

    $links = "http://www.campaignind ia.in/feature/analysis";
    $tag1 = '<div class=feature-wrapper>';
    $tag2 = '<h1><a href';
    $tag3 = "</a>";

    $op = doGetTags($link s,"START",$tag1 ,$tag2,$tag3,"E ND");

    return:
    The exploded array of the page of link

    <?php

    function doGetTags()
    {
    $expCounter = 0;
    $numargs = func_num_args() ;
    $toExplode_enco ded = array();
    $link="";
    $condentExplode d = "";
    if ($numargs <= 1)
    {
    echo "The argument can not be less that two";
    die();
    }
    $arg_list = func_get_args() ;
    for($i = 0; $i < $numargs; $i++)
    {
    if($i == 0)
    {
    $link = $arg_list[$i];
    $first_delimite r = $arg_list[++$i];
    for($j=$i; $j < count($arg_list ); $j++)
    {
    if($arg_list[$j] != "START" && $arg_list[$j] != "END")
    {
    $toExplode_enco ded[$expCounter] =
    htmlentities($a rg_list[$j]); // the tag to be exploded and striped
    with attributs
    $expCounter++;
    }
    }
    }

    }
    $aContext = array('http' =array('proxy' =>'tcp://
    192.168.10.131: 8020', 'request_fullur i' =True));
    $cxContext = stream_context_ create($aContex t);
    $fpcont= file_get_conten ts($link, False, $cxContext);


    foreach($toExpl ode_encoded as $key =$val)
    {
    $delimiter = '\''.$val.'\'';
    $condentExplode d = explode($delimi ter,$fpcont);
    print_r($conden tExploded);

    }

    }
    $links = "http://www.campaignind ia.in/feature/analysis";
    $tag1 = '<div class=feature-wrapper>';
    $tag2 = '<h1><a href';
    $tag3 = "</a>";

    $op = doGetTags($link s,"START",$tag1 ,$tag2,$tag3,"E ND");

    ?>

    when I pass the delimiter in explode(), directly for
    example $condentExplode d = explode('<div class=feature-wrapper>',
    $fpcont);
    it working perfectly,the array is exploded. Thanks for any help
  • Captain Paralytic

    #2
    Re: explode with the foreach not working (beginner)

    On 21 May, 09:30, sathyashrayan <asm_f...@yahoo .co.ukwrote:
    Dear group,
    The function to be used as follows:
    >
    $links = "http://www.campaignind ia.in/feature/analysis";
    $tag1 = '<div class=feature-wrapper>';
    $tag2 = '<h1><a href';
    $tag3 = "</a>";
    >
    $op = doGetTags($link s,"START",$tag1 ,$tag2,$tag3,"E ND");
    >
    return:
    The exploded array of the page of link
    >
    <?php
    >
    function doGetTags()
    {
    $expCounter = 0;
    $numargs = func_num_args() ;
    $toExplode_enco ded = array();
    $link="";
    $condentExplode d = "";
    if ($numargs <= 1)
    {
    echo "The argument can not be less that two";
    die();
    }
    $arg_list = func_get_args() ;
    for($i = 0; $i < $numargs; $i++)
    {
    if($i == 0)
    {
    $link = $arg_list[$i];
    $first_delimite r = $arg_list[++$i];
    for($j=$i; $j < count($arg_list ); $j++)
    {
    if($arg_list[$j] != "START" && $arg_list[$j] != "END")
    {
    $toExplode_enco ded[$expCounter] =
    htmlentities($a rg_list[$j]); // the tag to be exploded and striped
    with attributs
    $expCounter++;
    }
    }
    }
    >
    }
    $aContext = array('http' =array('proxy' =>'tcp://
    192.168.10.131: 8020', 'request_fullur i' =True));
    $cxContext = stream_context_ create($aContex t);
    $fpcont= file_get_conten ts($link, False, $cxContext);
    >
    foreach($toExpl ode_encoded as $key =$val)
    {
    $delimiter = '\''.$val.'\'';
    $condentExplode d = explode($delimi ter,$fpcont);
    print_r($conden tExploded);
    >
    }
    >
    }
    >
    $links = "http://www.campaignind ia.in/feature/analysis";
    $tag1 = '<div class=feature-wrapper>';
    $tag2 = '<h1><a href';
    $tag3 = "</a>";
    >
    $op = doGetTags($link s,"START",$tag1 ,$tag2,$tag3,"E ND");
    >
    ?>
    >
    when I pass the delimiter in explode(), directly for
    example $condentExplode d = explode('<div class=feature-wrapper>',
    $fpcont);
    it working perfectly,the array is exploded. Thanks for any help
    HAve you tried echoing the $delimiter variable just before the
    $condentExplode d = explode($delimi ter,$fpcont);
    statement (no of course I know you didn't, otherwise you would have
    seen what was wrong!)

    Try this foreach instead:
    foreach($toExpl ode_encoded as $key =$val)
    {
    $condentExplode d = explode($val,$f pcont);
    print_r($conden tExploded);

    }

    Comment

    • sathyashrayan

      #3
      Re: explode with the foreach not working (beginner)

      On May 21, 2:54 pm, Captain Paralytic <paul_laut...@y ahoo.comwrote:
      On 21 May, 09:30, sathyashrayan <asm_f...@yahoo .co.ukwrote:
      >
      >
      >
      Dear group,
      The function to be used as follows:
      >
      $links = "http://www.campaignind ia.in/feature/analysis";
      $tag1 = '<div class=feature-wrapper>';
      $tag2 = '<h1><a href';
      $tag3 = "</a>";
      >
      $op = doGetTags($link s,"START",$tag1 ,$tag2,$tag3,"E ND");
      >
      return:
      The exploded array of the page of link
      >
      <?php
      >
      function doGetTags()
      {
      $expCounter = 0;
      $numargs = func_num_args() ;
      $toExplode_enco ded = array();
      $link="";
      $condentExplode d = "";
      if ($numargs <= 1)
      {
      echo "The argument can not be less that two";
      die();
      }
      $arg_list = func_get_args() ;
      for($i = 0; $i < $numargs; $i++)
      {
      if($i == 0)
      {
      $link = $arg_list[$i];
      $first_delimite r = $arg_list[++$i];
      for($j=$i; $j < count($arg_list ); $j++)
      {
      if($arg_list[$j] != "START" && $arg_list[$j] != "END")
      {
      $toExplode_enco ded[$expCounter] =
      htmlentities($a rg_list[$j]); // the tag to be exploded and striped
      with attributs
      $expCounter++;
      }
      }
      }
      >
      }
      $aContext = array('http' =array('proxy' =>'tcp://
      192.168.10.131: 8020', 'request_fullur i' =True));
      $cxContext = stream_context_ create($aContex t);
      $fpcont= file_get_conten ts($link, False, $cxContext);
      >
      foreach($toExpl ode_encoded as $key =$val)
      {
      $delimiter = '\''.$val.'\'';
      $condentExplode d = explode($delimi ter,$fpcont);
      print_r($conden tExploded);
      >
      }
      >
      }
      >
      $links = "http://www.campaignind ia.in/feature/analysis";
      $tag1 = '<div class=feature-wrapper>';
      $tag2 = '<h1><a href';
      $tag3 = "</a>";
      >
      $op = doGetTags($link s,"START",$tag1 ,$tag2,$tag3,"E ND");
      >
      ?>
      >
      when I pass the delimiter in explode(), directly for
      example $condentExplode d = explode('<div class=feature-wrapper>',
      $fpcont);
      it working perfectly,the array is exploded. Thanks for any help
      >
      HAve you tried echoing the $delimiter variable just before the
      $condentExplode d = explode($delimi ter,$fpcont);
      statement (no of course I know you didn't, otherwise you would have
      seen what was wrong!)
      >
      Try this foreach instead:
      foreach($toExpl ode_encoded as $key =$val)
      {
      $condentExplode d = explode($val,$f pcont);
      print_r($conden tExploded);
      >
      }
      Thanks for your reply. Yes I did echo the $val and passed the $val
      directly to the explode as like above. It does not work. If I pass the
      direct html delimiter in the explode instead of $val it works. I
      really don't know why. pass-by reference or pass by value problem? I
      am using php5.

      Comment

      • Captain Paralytic

        #4
        Re: explode with the foreach not working (beginner)

        On 21 May, 11:12, sathyashrayan <asm_f...@yahoo .co.ukwrote:
        On May 21, 2:54 pm, Captain Paralytic <paul_laut...@y ahoo.comwrote:
        >
        >
        >
        On 21 May, 09:30, sathyashrayan <asm_f...@yahoo .co.ukwrote:
        >
        Dear group,
        The function to be used as follows:
        >
        $links = "http://www.campaignind ia.in/feature/analysis";
        $tag1 = '<div class=feature-wrapper>';
        $tag2 = '<h1><a href';
        $tag3 = "</a>";
        >
        $op = doGetTags($link s,"START",$tag1 ,$tag2,$tag3,"E ND");
        >
        return:
        The exploded array of the page of link
        >
        <?php
        >
        function doGetTags()
        {
        $expCounter = 0;
        $numargs = func_num_args() ;
        $toExplode_enco ded = array();
        $link="";
        $condentExplode d = "";
        if ($numargs <= 1)
        {
        echo "The argument can not be less that two";
        die();
        }
        $arg_list = func_get_args() ;
        for($i = 0; $i < $numargs; $i++)
        {
        if($i == 0)
        {
        $link = $arg_list[$i];
        $first_delimite r = $arg_list[++$i];
        for($j=$i; $j < count($arg_list ); $j++)
        {
        if($arg_list[$j] != "START" && $arg_list[$j] != "END")
        {
        $toExplode_enco ded[$expCounter] =
        htmlentities($a rg_list[$j]); // the tag to be exploded and striped
        with attributs
        $expCounter++;
        }
        }
        }
        >
        }
        $aContext = array('http' =array('proxy' =>'tcp://
        192.168.10.131: 8020', 'request_fullur i' =True));
        $cxContext = stream_context_ create($aContex t);
        $fpcont= file_get_conten ts($link, False, $cxContext);
        >
        foreach($toExpl ode_encoded as $key =$val)
        {
        $delimiter = '\''.$val.'\'';
        $condentExplode d = explode($delimi ter,$fpcont);
        print_r($conden tExploded);
        >
        }
        >
        }
        >
        $links = "http://www.campaignind ia.in/feature/analysis";
        $tag1 = '<div class=feature-wrapper>';
        $tag2 = '<h1><a href';
        $tag3 = "</a>";
        >
        $op = doGetTags($link s,"START",$tag1 ,$tag2,$tag3,"E ND");
        >
        ?>
        >
        when I pass the delimiter in explode(), directly for
        example $condentExplode d = explode('<div class=feature-wrapper>',
        $fpcont);
        it working perfectly,the array is exploded. Thanks for any help
        >
        HAve you tried echoing the $delimiter variable just before the
        $condentExplode d = explode($delimi ter,$fpcont);
        statement (no of course I know you didn't, otherwise you would have
        seen what was wrong!)
        >
        Try this foreach instead:
        foreach($toExpl ode_encoded as $key =$val)
        {
        $condentExplode d = explode($val,$f pcont);
        print_r($conden tExploded);
        >
        }
        >
        Thanks for your reply. Yes I did echo the $val and passed the $val
        directly to the explode as like above. It does not work. If I pass the
        direct html delimiter in the explode instead of $val it works. I
        really don't know why. pass-by reference or pass by value problem? I
        am using php5.
        Please show us the result of the echo.

        Comment

        • petersprc

          #5
          Re: explode with the foreach not working (beginner)

          On May 21, 5:30 am, sathyashrayan <asm_f...@yahoo .co.ukwrote:
          Dear group,
          The function to be used as follows:
          >
          $links = "http://www.campaignind ia.in/feature/analysis";
          $tag1 = '<div class=feature-wrapper>';
          $tag2 = '<h1><a href';
          $tag3 = "</a>";
          >
          $op = doGetTags($link s,"START",$tag1 ,$tag2,$tag3,"E ND");
          >
          return:
          The exploded array of the page of link
          >
          <?php
          >
          function doGetTags()
          {
          $expCounter = 0;
          $numargs = func_num_args() ;
          $toExplode_enco ded = array();
          $link="";
          $condentExplode d = "";
          if ($numargs <= 1)
          {
          echo "The argument can not be less that two";
          die();
          }
          $arg_list = func_get_args() ;
          for($i = 0; $i < $numargs; $i++)
          {
          if($i == 0)
          {
          $link = $arg_list[$i];
          $first_delimite r = $arg_list[++$i];
          for($j=$i; $j < count($arg_list ); $j++)
          {
          if($arg_list[$j] != "START" && $arg_list[$j] != "END")
          {
          $toExplode_enco ded[$expCounter] =
          htmlentities($a rg_list[$j]); // the tag to be exploded and striped
          with attributs
          $expCounter++;
          }
          }
          }
          >
          }
          $aContext = array('http' =array('proxy' =>'tcp://
          192.168.10.131: 8020', 'request_fullur i' =True));
          $cxContext = stream_context_ create($aContex t);
          $fpcont= file_get_conten ts($link, False, $cxContext);
          >
          foreach($toExpl ode_encoded as $key =$val)
          {
          $delimiter = '\''.$val.'\'';
          $condentExplode d = explode($delimi ter,$fpcont);
          print_r($conden tExploded);
          >
          }
          >
          }
          >
          $links = "http://www.campaignind ia.in/feature/analysis";
          $tag1 = '<div class=feature-wrapper>';
          $tag2 = '<h1><a href';
          $tag3 = "</a>";
          >
          $op = doGetTags($link s,"START",$tag1 ,$tag2,$tag3,"E ND");
          >
          ?>
          >
          when I pass the delimiter in explode(), directly for
          example $condentExplode d = explode('<div class=feature-wrapper>',
          $fpcont);
          it working perfectly,the array is exploded. Thanks for any help

          Comment

          • sathyashrayan

            #6
            Re: explode with the foreach not working (beginner)

            On May 21, 4:28 pm, petersprc <peters...@gmai l.comwrote:
            Hi,
            >
            You can avoid HTML encoding the value with htmlentities on the value
            and enclosing it in quotes... The explode function will want to match
            the string exactly.
            >
            If you just want the article links, you can use a simple regexp like
            [.. good reply snipped..]

            Thanks a lot for all of you who helped me to learn.

            Comment

            Working...