What is the Regex code to match a string in a homepage?

Discussion in 'PHP' started by surfmore, Aug 18, 2009.

  1. #1
    Hello,

    I am programming using php language and I want to find using regex the STRING inside the following:

    target="">STRING</a>

    What is the regex code to extract this string?

    I am using

    preg_match_all("$re", $html, $m, PREG_PATTERN_ORDER);

    where $re is the regex code I need.

    thanks.
     
    surfmore, Aug 18, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    $re = '/>([^<]+)<\/a>/i';
    
    PHP:
     
    premiumscripts, Aug 18, 2009 IP
  3. surfmore

    surfmore Peon

    Messages:
    118
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    But this code will not get the starting parameter that is target="">
     
    surfmore, Aug 18, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You said you wanted to find just the STRING part...

    $re = '/target=\'"([^\'"]+)\'"[^>]*>([^<]+)<\/a>/i';
     
    premiumscripts, Aug 18, 2009 IP
  5. DesignWeb

    DesignWeb Active Member

    Messages:
    251
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Here is one of my old function. (Returns the found results in array)
    function preg_match_all_array($startWith, $endWith, $source)
    	{
    		$findMatches = preg_match_all('/'.$startWith.'(.*?)'.$endWith.'/si',$source,$matches);
    		
    		if($findMatches)
    		{
    			$allmatches = array();
    			for($i=0;$i<count($matches[0]);$i++)
    			{
    				$allmatches[] = $matches[1][$i];
    			}
    			return $allmatches;
    		} else {
    			return false;
    		}
    	}
    
    PHP:
    Here is example usage (this will extract all links from Forums.digitalpoint.com)
    <?php
    $get_source = file_get_contents('http://forums.digitalpoint.com/');
    	$result = preg_match_all_array('href="', '"', $get_source);
    	echo '<pre>';
    	
    	print_r($result);
    ?>
    
    PHP:
     
    DesignWeb, Aug 18, 2009 IP
  6. surfmore

    surfmore Peon

    Messages:
    118
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    THANK YOU VERY MUCH FOR BOTH OF YOU!!

    Solved my problem!
     
    surfmore, Aug 18, 2009 IP