billatl 01-11-2010, 05:32 PM Hello,
I have some Wordpress code that goes through the source of a page and locates the first instance of <img to use as a thumbnail in other sections of the site. On a few pages, a header.gif image is actually teh first instance, so I'd want to get the second instance in these cases. Here's the existing code:
// Image extraction
$image = '';
$x = stripos($content, '<img');
if ($x !== false) {
$x = stripos($content, 'src="', $x);
if ($x !== false) {
$x += 5;
$y = strpos($content, '"', $x);
$image = substr($content, $x, $y-$x);
}
}
The "$x = stripos($content, '<img');" finds the first instance of <img and returns the result. I would like to modify this to get the 2nd image if the first image found is "header.gif".
Any suggestions on how to insert a For Loop (or something as good) to test for "header.gif" and move to the next image in these cases?
angst 01-11-2010, 06:09 PM this might not be the best way, but it works:
$content = "<head></head><body><html><p>some text...</p><img src=\"header.gif\" />blah blah blah<p><img src=\"someImage.jpg\" /></body></html>";
$image = '';
$x = stripos($content, '<img');
while($i <= 2){
if ($x !== false) {
$x = stripos($content, 'src="', $x);
if ($x !== false) {
$x += 5;
$y = strpos($content, '"', $x);
$image = substr($content, $x, $y-$x);
if($image !== "header.gif"){
break;
}
}
}
}
echo $image;
Rebbu 01-11-2010, 06:56 PM Just add another line to search for <img> againt after the first search. I added the extra line to your script, which you can see below.
<?php
// Image extraction
$image = '';
$x = stripos($content, '<img');
$x = stripos($content, '<img', $x+1); // This is the added line
if ($x !== false) {
$x = stripos($content, 'src="', $x);
if ($x !== false) {
$x += 5;
$y = strpos($content, '"', $x);
$image = substr($content, $x, $y-$x);
}
}
?>
JAY6390 01-11-2010, 07:40 PM Personally I'd use a regular expression to extract the src from the image
preg_match('/<img[^>]+\bsrc="([^"]+\.jpg)"[^>]>/', $content, $matches);
$image = $matches[1];
billatl 01-11-2010, 08:27 PM this might not be the best way, but it works:
$content = "<head></head><body><html><p>some text...</p><img src=\"header.gif\" />blah blah blah<p><img src=\"someImage.jpg\" /></body></html>";
$image = '';
$x = stripos($content, '<img');
while($i <= 2){
if ($x !== false) {
$x = stripos($content, 'src="', $x);
if ($x !== false) {
$x += 5;
$y = strpos($content, '"', $x);
$image = substr($content, $x, $y-$x);
if($image !== "header.gif"){
break;
}
}
}
}
echo $image;
This code doesn't work for me - it crashes PHP
billatl 01-11-2010, 08:29 PM Just add another line to search for <img> againt after the first search. I added the extra line to your script, which you can see below.
I don't follow this - where exactly is your code comparing to header.gif??
angst 01-11-2010, 08:30 PM ah, it just needs the counter to increment.
$content = "<head></head><body><html><p>some text...</p><img src=\"header.gif\" />blah blah blah<p><img src=\"someImage.jpg\" /></body></html>";
$image = '';
$x = stripos($content, '<img');
$i = 0;
while($i <= 2){
$i++;
if ($x !== false) {
$x = stripos($content, 'src="', $x);
if ($x !== false) {
$x += 5;
$y = strpos($content, '"', $x);
$image = substr($content, $x, $y-$x);
if($image !== "header.gif"){
break;
}
}
}
}
echo $image;
billatl 01-11-2010, 08:31 PM Personally I'd use a regular expression to extract the src from the image
preg_match('/<img[^>]+\bsrc="([^"]+\.jpg)"[^>]>/', $content, $matches);
$image = $matches[1];
I don't have a lot of experience with Regular Expressions. I understand your concept of matching the string form the src tag - but I'm not clear how to put this in the For Loop
JAY6390 01-11-2010, 08:53 PM You don't need the for loop. You literally just need that and your $content
Rebbu 01-11-2010, 10:16 PM I don't follow this - where exactly is your code comparing to header.gif??
Uh, compare the script you posted with the one I did. You'll notice the one I posted has an additional line...
billatl 01-11-2010, 10:29 PM Uh, compare the script you posted with the one I did. You'll notice the one I posted has an additional line...
Of course I see the extra line. Perhaps I should re-phrase my statement: "I don't understand your code". Your code:
$x = stripos($content, '<img', $x+1); // This is the added line
If $x equals the position of "<img" in the string, how does adding 1 to $x compare it to header.gif and account for the fact that some posts have header.gif and other don't??
Rebbu 01-12-2010, 12:46 AM Oh my apologies, I didn't read the original post properly. I didn't realize you wanted to skip the first instance of "<img" only IF the src for that img was header.gif.
Could do something like this:
$headsrc = stripos("header.gif");
if ($headsrc===false){
$x = stripos($content, '<img');
}else{
$first = stripos($content, '<img');
$second = stripos($content, '<img', $first + 1);
if (($headsrc > $first) && ($headsrc <$second)){//there between the few tags, so it must be in the first img (of course there are problems with this)
$x = $second;
}else{
$x = $first;
}
}
|
|