Regular Expressions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rats
Forum Newbie
Posts: 21
Joined: Fri May 31, 2002 5:55 am

Regular Expressions

Post 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 .
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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?
rats
Forum Newbie
Posts: 21
Joined: Fri May 31, 2002 5:55 am

Post by rats »

Thats exactly what I wanted Thanks

There wasnt any particular error.
Post Reply