0

please help me. i got error log after upgrade php from 7.4 to 8.1 :

PHP Warning: Undefined array key 0

this is the script code, please help me, how to fix this

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches);
  $tanpahttp = array("http://","https://");
  $gambar1 = $matches[1][0];
  $gambar2 = str_replace($tanpahttp,"",$matches[1][0]);
  $first_img = $gambar2;
  
  if(empty($first_img)) {
    $first_img = "https://kbimages.dreamhosters.com/images/Site_Not_Found_Dreambot.fw.png";
  }
  return $first_img;
}

it fine in 7.4 but i must upgrade mine

im very confused, im was weak from this coding for php 8.1

but if i running in 7.4 my site is fine, in php8.0 it error log very long and my cpu is spike high. i just want to grab image from my first image in my wordpress site become my feature post thumbnail

  • Don't use regular expressions to parse HTML, use `DOMDocument`. – Barmar May 31 '23 at 18:21
  • Please add the value of `$post->post_content` that gets this error – Barmar May 31 '23 at 18:22
  • The error indicates that the regexp didn't match anything, so all the arrays are empty. – Barmar May 31 '23 at 18:23
  • I doubt it was actually "fine" in 7.4. The defaults for `error_reporting()` probably changed when you upgraded, so a silent error now prints a warning. – Barmar May 31 '23 at 18:24
  • hallo i already edit my question, please help me what the best script to fix this, please help me. i just want to grab first external image url from my wordpress post – tech bandit May 31 '23 at 18:45
  • What if there's no image in the post? – Barmar May 31 '23 at 18:52
  • im using this script : ``` if(empty($first_img)) { $first_img = "https://kbimages.dreamhosters.com/images/Site_Not_Found_Dreambot.fw.png"; } return $first_img;``` – tech bandit May 31 '23 at 19:03
  • If `$first_img` isn't empty, you'll return it as is. If it doesn't contain ` – Barmar May 31 '23 at 19:04
  • I don't see the question edit. – Barmar May 31 '23 at 19:05
  • the specific error is caused from this line $gambar1 = $matches[1][0]; what is the best change for this? in my wordpress is doing fine, the image is fine. but in error log shown that – tech bandit May 31 '23 at 19:18
  • Check if `$output` is true before trying to use `$matches`. – Barmar May 31 '23 at 19:19
  • the output is like what i need it shown all link that exist in my wordpress, then i need just first link so i using $matches[1][0] and the result of $gambar1 is what i need. i add $gambar2 = str_replace($tanpahttp,"",$matches[1][0]); to remove http or https and the result of $first_img is what im expected, but why error_log file said it was error? – tech bandit May 31 '23 at 19:27
  • the error is came from this line: `$gambar1 = $matches[1][0]; ` and `$gambar2 = str_replace($tanpahttp,"",$matches[1][0]); ` i believe it came from [0] how to define the first link only because the $matches will show all the link, i just want to grab one link only – tech bandit May 31 '23 at 19:30
  • Put all the code that uses `$matches` inside `if ($output) { ... }` – Barmar May 31 '23 at 19:47

0 Answers0