The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 22, 2019, 04:50:32 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Regex explenation needed  (Read 2894 times)
Phaze
Rookie
**
Offline Offline

Posts: 12


View Profile
« on: January 29, 2010, 05:55:37 PM »

Need help to understand how to use regex to get pruductID and image url from string.
I manage to get it one by one, but i want to have one regex for it so it come in order in the array
when i use preg_match......

Code:
                       
   <div class="thumbnailbox">
     <a href="getboxnow.php?pruductid=panther" target="_top">                               
     <img src="http://www.site.com/tmbImage.jpg" alt="panther" title="panther" border="0" height="75" width="100"></a>           
   </div> 
Logged

No links in signatures please
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #1 on: January 29, 2010, 09:26:43 PM »

Need help to understand how to use regex to get pruductID and image url from string.
I manage to get it one by one, but i want to have one regex for it so it come in order in the array
when i use preg_match......

Code:
                       
   <div class="thumbnailbox">
     <a href="getboxnow.php?pruductid=panther" target="_top">                               
     <img src="http://www.site.com/tmbImage.jpg" alt="panther" title="panther" border="0" height="75" width="100"></a>           
   </div> 

So you want 'panter' in this example?

It can be a bear.

What I almost always do is get ALL the href's on the page, then filter out the one I want.

href="getboxnow.php?pruductid=panther"

So, in perl...

while($content_of_page =~ /href="(.*?)"/sig) {
  $thisLink = $1;
  print "$thisLink\n";
}

That should print every href tag on the page.

Next, I would probably eliminate those that do not contain "productid="

while($content_of_page =~ /href="(.*?)"/sig) {
  $thisLink = $1;
  unless($thisLink =~ /productid=/) { next; }
  print "$thisLink\n";
}

See where I'm going?

This is my main, most used, most reliable method.

If I am feeling bold, I might put getboxnow.php? in the first while loop:

while($content_of_page =~ /href="getboxnow.php?(.*?)"/sig) {
  $thisLink = $1;
  print "$thisLink\n";
}

or something like that.  Untested, have a ball.

Bompa

PS: On each new CMS, I grab the source code and practice offline
which saves a lot of time.

Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
Phaze
Rookie
**
Offline Offline

Posts: 12


View Profile
« Reply #2 on: January 30, 2010, 12:27:16 AM »

yes it's panther and http://www.site.com i want to put in an array with preg_match_all().
And i have no problem to get all productsID or url into an array it's when i want to put both
in the same array with preg_match_all() i got problem.
Now i don't know if thats possible but i think it's possible to do it with one regex call.
Every tutorial i find explain how to get all url's but no one explain how to parse two values
in one query.

I need something like this:

Code:
<div class="thumbnailbox"><a[^>]+?href="([^"]+)".*?<img[^>]+?src="([^"]+\.jpg)"<\/div>

But all i get is emty arrays.
« Last Edit: January 30, 2010, 12:45:58 AM by Phaze » Logged

No links in signatures please
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #3 on: January 30, 2010, 04:41:43 PM »

yes it's panther and http://www.site.com i want to put in an array with preg_match_all().
And i have no problem to get all productsID or url into an array it's when i want to put both
in the same array with preg_match_all() i got problem.

OH, well, whatever is within the first set of parentheseses will be in $1; second set
of parentheseses $2, etc.

I don't know how to actually get them into an array with php, but in perl we use push.

push(@array, "$1 $2");


Bompa
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
Phaze
Rookie
**
Offline Offline

Posts: 12


View Profile
« Reply #4 on: January 30, 2010, 05:41:34 PM »

I solved the problem after some trial and error. Thanx for your time
Logged

No links in signatures please
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!