A preg_match_all problem. Looking for co-operation
Moderator: General Moderators
A preg_match_all problem. Looking for co-operation
Html Code:
<div class="result5464">
<a href="http://myserverserver.com/devnetwork" rel="nofollow"><img src="http://whatever.com/deevnet.jpg" alt="Dev Network"></a>
</div>
<div class="result3546">
<a href="http://myserverserver.com/soul_fly" rel="nofollow"><img src="http://whatever.com/soul.jpg" alt="soul_fly"></a>
</div>
My target is : http://myserverserver.com/devnetwork
http://myserverserver.com/soul_fly
I tried:
preg_match_all('/<a href=[\"']([^<]+)[\"']<\/a>/', $str, $matches);
echo $matches[0][0]."</br>";
echo $matches[0][1]."</br>";
Not worked. plz co-operate me.
In addition, I would like to know where can I get detailed explanation of those pattern like ([^<]+) or others.
<div class="result5464">
<a href="http://myserverserver.com/devnetwork" rel="nofollow"><img src="http://whatever.com/deevnet.jpg" alt="Dev Network"></a>
</div>
<div class="result3546">
<a href="http://myserverserver.com/soul_fly" rel="nofollow"><img src="http://whatever.com/soul.jpg" alt="soul_fly"></a>
</div>
My target is : http://myserverserver.com/devnetwork
http://myserverserver.com/soul_fly
I tried:
preg_match_all('/<a href=[\"']([^<]+)[\"']<\/a>/', $str, $matches);
echo $matches[0][0]."</br>";
echo $matches[0][1]."</br>";
Not worked. plz co-operate me.
In addition, I would like to know where can I get detailed explanation of those pattern like ([^<]+) or others.
Re: A preg_match_all problem. Looking for co-operation
is there none to help a friend 
Re: A preg_match_all problem. Looking for co-operation
Try this:
And yeah, this is good too: http://www.addedbytes.com/cheat-sheets/ ... eat-sheet/
Code: Select all
preg_match_all('/href="([^"]+)"/i', $str, $matches);
print_r($matches);Re: A preg_match_all problem. Looking for co-operation
jackpf wrote:Try this:
Code: Select all
preg_match_all('/href="([^"]+)"/i', $str, $matches); print_r($matches);
didn't work
Re: A preg_match_all problem. Looking for co-operation
Works for me.
I get
I get
Code: Select all
Array
(
[0] => Array
(
[0] => href="http://myserverserver.com/devnetwork"
[1] => href="http://myserverserver.com/soul_fly"
)
[1] => Array
(
[0] => http://myserverserver.com/devnetwork
[1] => http://myserverserver.com/soul_fly
)
)Re: A preg_match_all problem. Looking for co-operation
jackpf wrote:Works for me.
I getCode: Select all
Array ( [0] => Array ( [0] => href="http://myserverserver.com/devnetwork" [1] => href="http://myserverserver.com/soul_fly" ) [1] => Array ( [0] => http://myserverserver.com/devnetwork [1] => http://myserverserver.com/soul_fly ) )
Could you plz check the syntax again:
preg_match_all('/href="([^"]+)"/i', $str, $matches);
It returns me blank page. sorry i'm still newbie in regex
Re: A preg_match_all problem. Looking for co-operation
Did you copy my exact code and use print_r()??
Re: A preg_match_all problem. Looking for co-operation
yup.jackpf wrote:Did you copy my exact code and use print_r()??
Re: A preg_match_all problem. Looking for co-operation
The complete script:
Edit: This post was recovered from search engine cache.
Code: Select all
<?php
header('Content-Type: text/plain');
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
ini_set('html_errors', false);
$str = <<<HTML
<div class="result5464">
<a href="http://myserverserver/devnetwork" rel="nofollow"><img src="http://whatever/devnet.jpg" alt="Dev Network"></a>
</div>
<div class="result3546">
<a href="http://myserverserver/soul_fly" rel="nofollow"><img src="http://whatever/soul.jpg" alt="soul_fly"></a>
</div>
HTML;
// jackpf's solution
preg_match_all('/href="([^"]+)"/i', $str, $matches);
echo 'Matches: ';
print_r($matches);
?>
Last edited by McInfo on Thu Jun 17, 2010 12:30 pm, edited 1 time in total.
Re: A preg_match_all problem. Looking for co-operation
Well yeah, I just assumed he would put his own HTML in there...
Honestly.
Honestly.
Re: A preg_match_all problem. Looking for co-operation
You know the old saying about what happens when you "ass+u+me"...
It's not obvious how the HTML is being acquired by the script or even if the HTML is making into the $str variable. I thought it would be better to just put it all together so there are fewer assumptions about what is being done on the other end of the conversation.
I added error reporting to the script so any oddities should show themselves, such as using the wrong variables or something (which is what I assume the problem is).
Edit: This post was recovered from search engine cache.
It's not obvious how the HTML is being acquired by the script or even if the HTML is making into the $str variable. I thought it would be better to just put it all together so there are fewer assumptions about what is being done on the other end of the conversation.
I added error reporting to the script so any oddities should show themselves, such as using the wrong variables or something (which is what I assume the problem is).
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:31 pm, edited 1 time in total.
Re: A preg_match_all problem. Looking for co-operation
McInfo thanks for reply. Could I plz pm you?
Re: A preg_match_all problem. Looking for co-operation
ok lets be more practical. I was actually trying to grab urls from a webpage.
For example, I'm trying to grab URLs from http://forums.devnetwork.net (specifically which are in <a href"--------target----------")
So, I wrote this php codes:
but output returns me
Array ( [0] => Array ( ) [1] => Array ( ) ) grrrr
For example, I'm trying to grab URLs from http://forums.devnetwork.net (specifically which are in <a href"--------target----------")
So, I wrote this php codes:
Code: Select all
<? php
$ch = curl_init();
$target = "http://forums.devnetwork.net";
curl_setopt($ch, CURLOPT_URL, $target);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$str = curl_exec($ch);
preg_match_all('/href="([^"]+)"/i', $str, $matches);
print_r($matches);
?>
Array ( [0] => Array ( ) [1] => Array ( ) ) grrrr
Re: A preg_match_all problem. Looking for co-operation
After the
which should be changed to
add these two lines.
Edit: This post was recovered from search engine cache.
Code: Select all
<? phpCode: Select all
<?phpCode: Select all
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
Last edited by McInfo on Thu Jun 17, 2010 12:32 pm, edited 1 time in total.