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
Regular Expression Help
Old 12-24-2010, 04:23 AM Regular Expression Help
Novice Talker

Posts: 5
Trades: 0
Hey guys,

I've been stuck on this for a while. I'm trying to make a few different PHP scripts to accomplish similar things. For the first one, I wanted to be able to take in a URL and extract a small piece of data from it. For example:

Code:
http://www.somewebsite.com/123456_1234567890123_1234567890_12345678_1234567_n.jpg
From this, I want to extract the "12345678" portion of this URL. It will always be the third to last fragment (separated by _) from the file extension. The fragments can be any length, the last one always being an "n". I have no idea where to start for this task.

For my second task, I'm trying to rip all the image tags out of some source code. I don't want to include the images if the source URL is from a specific website. I have some code working that rips all of the image tags from a site, but can't figure out how to make it not include ones from the "banned" site. Here's what I have:

PHP Code:
<?php 

$url 
"example.com"

$text file_get_contents($url); 

preg_match_all("/<img[^>]+\>/i"$text$match); 
print_r($match);

?>
So say I had a site called banned.com and didn't want to include in my search any image tags like <img src="banned.com/blahblah.jpg" />. Would RegExp's be the best choice for this? I've heard somewhere that it's not good for omitting search results.

Thanks in advance and happy holidays.
Atlos is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-24-2010, 06:32 AM Re: Regular Expression Help
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Whenever I have to parse html or xml I try as much as I can to avoid using regular expressions. I think using DOM is much more reliable:
http://php.net/manual/en/book.dom.php

PHP Code:
$dom = new DOMDocument();
$dom->loadHTML($url);

foreach(
$dom->getElementsByTagName('img') as $image)
{
     if(
$image->hasAttributes())
     {
          if(
strpos($image->getAttribute('src'), 'bannedsite.com') !== false)
               
$image->setAttribute('src''');
     }

__________________

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 online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 12-24-2010, 06:57 AM Re: Regular Expression Help
Novice Talker

Posts: 5
Trades: 0
Interesting, I'll look into this and report back. Thanks!
Atlos is offline
Reply With Quote
View Public Profile
 
Old 12-24-2010, 05:08 PM Re: Regular Expression Help
Novice Talker

Posts: 5
Trades: 0
I just tried the following to see if any images would print out and got a bunch of errors instead:

PHP Code:
<?php 

$url 
"http://www.example.com"
$text file_get_contents($url); 
$dom = new DOMDocument();
$dom->loadHTML($text);
$pageimages $dom->getElementsByTagName('img');
foreach(
$pageimages as $image){
    echo 
$image->nodeValue;
}

?>
Some errors I got were things like:

Code:
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID post598834833 already defined in Entity, line: 1247 in /home/<id>/public_html/imagegrab.php on line 16
which is referring to:
PHP Code:
$dom->loadHTML($text); 
Any ideas? On a side note, it's tough to write OOP without an IDE. Does anybody know of a free PHP IDE that has autocomplete, etc like Eclipse? I'm used to Eclipse for Java which is really nice, and wasn't sure of the functionality of their PHP plugin or w/e.

Last edited by Atlos; 12-24-2010 at 05:10 PM..
Atlos is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Regular Expression Help
 

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.21284 seconds with 12 queries