Page 1 of 1

Regular Expressions

Posted: Tue Jul 16, 2002 6:43 pm
by rats
I an having trouble finding which reglar expression suits my needs.

I want to search through a web page a pull out all the links. Everything from the starting < a to the closing < / a >

ive been trying

Code: Select all

preg_match_all("/(<a )(.*)(<\/a>)/i", $web_page, $matches);

for($i=0; $i< count($matches&#1111;0]); $i++) &#123;
        echo( $matches&#1111;0]&#1111;$i]);
        echo( $matches&#1111;1]&#1111;$i]);
&#125;
The expression is not quite how i want it but i get an error if i change it. Since preg_match_all is a PCRE the syntax of the expression is different to normal reg ex's .

Posted: Wed Jul 17, 2002 4:15 am
by gnu2php
Is this any better?

Code: Select all

preg_match_all("/(<a &#1111;^>]+>)(.*)(<\/a>)/i", $web_page, $matches);
How about this?

Code: Select all

$links = array();

preg_replace_callback("/(<a &#1111;^>]+>)(.*)(<\/a>)/i",
	'callback_func', $web_page);

var_dump($links);


function callback_func($matches)
&#123;
	array_push($GLOBALS&#123;'links'&#125;, array($matches&#1111;1],
		$matches&#1111;2], $matches&#1111;3]));

	return $matches&#1111;0]; // So we don't alter the data
&#125;
By the way, what's the source code that caused the error you mentioned?

Posted: Sat Jul 20, 2002 2:45 am
by rats
Thats exactly what I wanted Thanks

There wasnt any particular error.