Before I started on PHP/SQL about 6 months ago, I had mastered VB6 coding, so I have taken to PHP/SQL very, very well, and have coded many fully functional CMS websites etc.

In VB6, there is a function called "GetBetween".

Here is an example of this function in VB6:


Private Sub form_load()
html = Inet1.Execute("http://google.com")
GoogleBanner = GetBetween(html, "<img src=", ">")

What that would do, is set the HTML of Google.com to the var named "html".

Then, it would get what is between 2 peice of said HTML code, within the "html" var, and set it to a var. As PHP programmers, this should be easy for you guys to understand.

Is there anyway to do this kind of thing in PHP?

  • Mike

    Hey.

    I do not see any need for such a function.
    I wouldnt have any practical use for it, as I can think of.
    And sure dont know what gain should be from my code below.

    I do not know if exists something like it in php,
    but this could easily be done without any function

    <?php
    
    $imgurl = "http://www.google.com/logo.gif";
    $google_banner = "<img src='" . $imgurl . "'>";
    echo $google_banner;
    
    ?>

      The point of the function would be to extract information from other pages on the web... Your way off. 😛

        One way would be to use regular expressions.
        Another way would be to write a function using PHP's string handling functions:

        function getBetween($str, $start, $end) {
        	$startlen = strlen($start);
        	if (($startpos = strpos($str, $start)) !== false
        		&& ($endpos = strpos($str, $end)) !== false
        		&& ($skip = $startpos + $startlen) <= $endpos) {
        		return substr($str, $skip, $endpos - $skip);
        	} else {
        		return false;
        	}
        }

        EDIT:
        though in the end you need to know what level of sophistication is required - e.g. do you need it to be case-insensitive, how to react if the delimiters are not found or if there are repeated delimiters etc. For more complex requirements, a regex version will come in handy.

          MHeys wrote:

          The point of the function would be to extract information from other pages on the web... Your way off. 😛

          Who put me way off?

          Sure wasnt me.
          If halojoy gets way off, there can plenty of people set way off by someone,
          while he himself is laughing being with a few on the highway.

          :mad: 😕 :mad:

          .

            Thanks a lot laserlight, but there is one problem there.

            The function you created uses $str as the variable to take the information from, and getbetween it. I need $str to be the html code of a website I define.

            • Mike

              The function you created uses $str as the variable to take the information from, and getbetween it. I need $str to be the html code of a website I define.

              Um, but what's the problem? Cant you just use the function?

                I am thinking that you can do it with fopen... I think that you can pass a URL to it...

                Well, if reading the webpage is the problem, then [man]file_get_contents/man would be a solution.

                EDIT:
                if I remember correctly someone had a problem with this suggestion because allow_url_fopen wrapper was not on, so you have to check that this is allowed.

                  Here's an example of using your function:

                  
                  This is the file called "gb.inc.php"
                  
                  <?php
                  
                   function getBetween($str, $start, $end) {
                      $startlen = strlen($start);
                      if (($startpos = strpos($str, $start)) !== false
                          && ($endpos = strpos($str, $end)) !== false
                          && ($skip = $startpos + $startlen) <= $endpos) {
                          return substr($str, $skip, $endpos - $skip);
                      } else {
                          return false;
                      }
                  }  
                  
                  ?>
                  
                  
                  
                  This is the file called "testgb.php"
                  
                  <?php
                  
                  include("gb.inc.php");
                  
                  $str = "test -- function -- test";
                  $start = "test -- ";
                  $end = " -- test";
                  
                  $result = getBetween($str, $start, $end);
                  
                  echo $result;
                  
                  ?>
                  
                  

                  This will echo "function". I want to capture the HTML source of a page into $str.

                  • Mike

                    hmm... come to think of it, strpos() wont work out for a complex example like google.com's homepage. You'll have to use regex:

                    function getBetween($str, $start, $end) {
                    	$matches = array();
                    	if (preg_match('/' . $start . '(.*?)'. $end . '/', $str, $matches)) {
                    		return $matches[1];
                    	} else {
                    		return false;
                    	}
                    }

                    An example of use:

                    $html = file_get_contents('http://www.google.com');
                    if (($banner = getBetween($html, '<img src=', '>')) !== false) {
                    	echo htmlspecialchars($banner);
                    } else {
                    	echo "Not Found";
                    }
                      Write a Reply...