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:
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.
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
$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.
------------------------------------------------------------
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.
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.