-1

I am trying to create a search and replace function I will be using during a data import to replace img tags that have a string in the url of the src. The first tag is not always the correct one, so the function I created does not work in more than half the results. In some cases instead of an tag the client for some reason used an , so the only consistent part is the the url path. The goal here is to match any or html tag that has a src that thats path contains "/dropcaps_final/.

These img and input tags are image letters e.g. a.png, b.png, c.png etc. The goal of the function is to replace these with actual text. So you will see the current function takes the file name, removed the extention and you are then left with the letter in lowercase, then I uppercase it before finally replacing it and returning the content for import.

Just a side note, the first img or input found in more than half the cases is not the img being used as a letter, another reason the preg_match function I made doesnt work.

function img_replace_with_text($str = null) {
  if( !empty($str) ) {

    $pattern = '/<img\s*(?:class\s*\=\s*[\'\"](.*?)[\'\"].*?\s*|src\s*\=\s*[\'\"](.*?)[\'\"].*?\s*|alt\s*\=\s*[\'\"](.*?)[\'\"].*?\s*|width\s*\=\s*[\'\"](.*?)[\'\"].*?\s*|height\s*\=\s*[\'\"](.*?)[\'\"].*?\s*)+.*?>/si';

    if(preg_match($pattern, $str, $matches) !== false){

      $url = $matches[2];
      $key = 'dropcaps_final';
      if (strpos($url, $key) != false) {
        $fileName = basename($url);
        $fileNameNoExtension = preg_replace("/\.[^.]+$/", "", $fileName);
        $letter = strtoupper($fileNameNoExtension);

        $new_str = str_replace($matches[0], $letter, $str);

        return $new_str;
      } else {
        return $str;
      }
    } else {
      return $str;
    }
  }
}

I need a function that finds any and all cases of and tags that have an html attribute src path containing "/dropcaps_final/" and then replacing the whole img/input tag with the files name within the src (excluding the extension).

Kyle Knight
  • 19
  • 1
  • 4

1 Answers1

-1
<?php
   $matches = array();
   $p = '/<img\s*(?:class\s*\=\s*[\'\\"](.*?)[\'\\"].*?\s*|src\s*\=\s*[\'\\"] 
   (.*?)[\'\\"].*?\s*|alt\s*\=\s*[\'\\"](.*?)[\'\\"].*?\s*|width\s*\=\s* 
   [\'\\"](.*?)[\'\\"].*?\s*|height\s*\=\s*[\'\\"](.*?)[\'\\"].*?\s*)+.*? 
   >/si';
   $fileName = 'your file name you want';
   $str = ' the string that you want to work on and replace its content'; 

    $p_c = array(
                  $p => function($match) {
                   $ret = str_replace("dropcaps_final", $fileName, $match[0]);
                   return $ret;
                  }
                );

    $res3 = preg_replace_callback_array($p_c, $str);

preg_replace_callback_array ( array $patterns_and_callbacks , mixed $subject [, int $limit = -1 [, int &$count ]] ) : mixed

patterns_and_callbacks

An associative array mapping patterns (keys) to callbacks (values).

subject

The string or an array with strings to search and replace.

limit

The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

count

If specified, this variable will be filled with the number of replacements done.

Klienblat Moshe
  • 322
  • 1
  • 6