DPChallenge: A Digital Photography Contest You are not logged in. (log in or register
 

DPChallenge Forums >> General Discussion >> Any PHP Gurus out there?
Pages:  
Showing posts 1 - 8 of 8, (reverse)
AuthorThread
08/23/2009 10:52:06 PM · #1
I need to pull part of a string out of some data and am trying to figure out the PHP code to do it. I need the contents of the src attribute of an HTML image tag. This could be any length, so I can't just count character positions.

Here are some examples, with the string I need extracted in bold:


This is a picture



So all I need is everything between the quotes of the src attribute of any given image tag.

Any help is appreciated. I am heading out for a bit will check back later. I hope I provided enough info.
08/23/2009 11:50:35 PM · #2
Hi,

First you need to open your.html file with some thing like that:

$URL = "//..../your.html";

$fp = fopen($URL,"r");
while (!feof($fp))
{
$line = fgets($fp, 4096);
After that you need to use preg_match_all function to parse the line.
}

Here a very good explanation of regex (regular expression) and preg_match_all function. Very helpful for me 4 mounts ago.

Here a very good software to test the regex.

Sorry for my English my first language is french.

Best Regards,

Yvan Bourassa
08/24/2009 05:35:40 AM · #3
Thanks Yvan - the data is coming out of a DB and I already know how to get at that part - the parsing is what I needed help on and you helped point me in the right direction with preg_match_all. I found a short function that I was able to modify for my use.

------------------------------------------------------------------
// Returns an array of strings where the start and end are found
function findinside($start, $end, $string) {
preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)'. preg_quote($end, '/').'/i', $string, $m);
return $m[1];
}

$string = $content_record['content'];

\\ since there is only going to be one img src reference in the $string, I will use the image's extension as the end string for the function to stop looking. Then I append it back on $slide_img

if(strpos($string,".jpg")==true) { $img_type = ".jpg"; }
elseif(strpos($string,".gif")==true) { $img_type = ".gif"; }
elseif(strpos($string,".png")==true) { $img_type = ".png"; }

$start = "src=\""; // find instances of the src attribute
$end = $img_type; // stop when you hit the first image extension

$out = findinside($start, $end, $string);

$slide_img = $out[0].$img_type; // I don't need to loop through the array - just need the first one out and append the image file extension back on.
------------------------------------------------------------

Thanks again.
08/24/2009 06:45:11 AM · #4
What language is this?
08/24/2009 07:41:40 AM · #5
If there's only going to be 1 tag in $content_record['content'], why not just use substr and strpos? You don't need a fancy regexp to do that.
08/24/2009 11:22:02 AM · #6
Sorry link missing!

The language is PHP , it is a server side language for web application. You can use it on your computer with EasyPHP.

Here a very good explanation of regex (regular expression) and preg_match_all function. Very helpful for me 4 mounts ago. //www.the-art-of-web.com/php/parse-links/

Here a very good software to test the regex.
//weitz.de/regex-coach/
08/24/2009 11:53:43 AM · #7
Can't you just use dynamic HTML via javascript to navigate through all the elements and pick out the "src" attribute?

Here's some pseudocode:

var myImage = document.all.img;
var imageSource = myImage.getAttribute("src");

The first line is not exact. You'll probably want to retrieve all the elements into an array and then iterate through each one. At the end, the imageSource variable contains a string value that is the source of the image.

Message edited by author 2009-08-24 11:54:40.
08/24/2009 12:48:04 PM · #8
Originally posted by AperturePriority:

Can't you just use dynamic HTML via javascript to navigate through all the elements and pick out the "src" attribute?

Because the application is already in PHP, and the string I am searching in is in a PHP var (pulled from a MySQL DB). I'm doing some other stuff in javascript though and your suggestion might come in handy. Thanks, Les.

Originally posted by langdon:

If there's only going to be 1 tag in $content_record['content'], why not just use substr and strpos? You don't need a fancy regexp to do that.

Because sometimes I just like to break out my top hat and fancy regexp. ;-) Actually, I am not sure how I would use substr since I don't know the string I am looking for and the src string (image path) length is variable ( /images/something.gif, /images/sub/deeper/evendeeper/pic.jpg, etc.)

The function I posted works perfectly albeit it may not be the most streamlined. Thanks all and thanks for the links, Yvan.
Pages:  
Current Server Time: 04/23/2024 04:30:51 AM

Please log in or register to post to the forums.


Home - Challenges - Community - League - Photos - Cameras - Lenses - Learn - Prints! - Help - Terms of Use - Privacy - Top ^
DPChallenge, and website content and design, Copyright © 2001-2024 Challenging Technologies, LLC.
All digital photo copyrights belong to the photographers and may not be used without permission.
Current Server Time: 04/23/2024 04:30:51 AM EDT.