Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
preg_match_all ( not quite there )
Old 10-16-2011, 10:09 AM preg_match_all ( not quite there )
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
Hi all

Im trying to extract just the src url of img tag from a page.

my preg_match_all is

preg_match_all('/(src)="(.*?)"/i',$f, $m);

BUT its returning

an array with 3 types of match :

1 - with src= IE src="graphics/homeandawaybanner.jpg"

2 - only the tx src IE src

&
( what i actually want )

the url IE graphics/homeandawaybanner.jpg

How do i adjust my preg to only extarct the url from src

Hope someone can help with this


Cheers
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
 
Register now for full access!
Old 10-16-2011, 10:17 AM Re: preg_match_all ( not quite there )
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
If the third element of the array is what you are looking for, can't you just take that value from the array?
PHP Code:
preg_match_all('/(src)="(.*?)"/i',$f$m);

$src $m[2]; 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-16-2011, 10:24 AM Re: preg_match_all ( not quite there )
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
No its actually creating 27 elements in 1 array representing 9 images on the page i am scraping

first 9 are in format src="url"
second 9 ar just src
and the third 9 are the urls i require.
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 10-16-2011, 10:27 AM Re: preg_match_all ( not quite there )
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
sorry my fault. I was mis interpreting the array.
Thanks. Your absolutely correct in the solution you offered.

Cheers
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 10-16-2011, 10:29 AM Re: preg_match_all ( not quite there )
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Edit
In that case disregard this.

Try:
PHP Code:
preg_match_all('/(src)="(.*?)"/i',$f$mPREG_SET_ORDER); 
Using the above the third element of the array should contain an array of URLs.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-16-2011, 10:39 AM Re: preg_match_all ( not quite there )
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
Yur thats the preg had started to use. I have just realized that it actually returns everything with a src attr which isnt actually as want

so
have amended to be

preg_match_all('/(<img src)="(.*?)"/i',$f, $m);

which appears to be working

Cheers
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 10-16-2011, 04:45 PM Re: preg_match_all ( not quite there )
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
If you're only interested in the url, I do believe you can also skip the parentheses around <img src.
Right now your regexp will match three things:

1) The complete matched string, <img src="some_url"
2) The first parentheses, <img src
3) The second parentheses, some_url

And from my understanding you don't need 2).

Edit:
Also, I think the questionmark inside the seccond parentheses is unnecessary. The * already matches zero or more.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 10-16-2011 at 04:50 PM..
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-16-2011, 05:02 PM Re: preg_match_all ( not quite there )
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I don't recommend using regexs at all if you're trying to parse content out of an html file. Using a DOM parser can be much quicker and less error prone.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 10-16-2011 at 06:37 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-16-2011, 05:09 PM Re: preg_match_all ( not quite there )
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
That looks really handy, Null! I didn't know about that, I guess I should read more PHP.net Thanks for posting that link
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 10-17-2011, 08:53 AM Re: preg_match_all ( not quite there )
Novice Talker

Posts: 6
Trades: 0
have you tried with htmlsimple dom , simplehtmldom[dot]sourceforge[dot]net/manual.htm

it is good too.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
dinhtoan is offline
Reply With Quote
View Public Profile Visit dinhtoan's homepage!
 
Reply     « Reply to preg_match_all ( not quite there )
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30310 seconds with 12 queries