You are here:  » Image Replacement with Reg Exp


Image Replacement with Reg Exp

Submitted by BobL on Sat, 2016-04-02 19:38 in

Hi David,

Hope all is well there.

Have a question if it would be possible to have a select field on a search reg exp filter, say to select image, brand or product name.

and if that Exp is there, say like (cloudfront) to have the mod replace that entire image url with one of our own?
Example images/noimage.jpg

Thanks in advance,
Your support is the greatest.
Bob L.

Submitted by support on Mon, 2016-04-04 09:03

Hello Bob,

To add a Search and Replace All RegExp filter, add the following code to your includes/filter.php:

  /*************************************************/
  /* searchReplaceAllRegExp */
  /*************************************************/
  $filter_names["searchReplaceAllRegExp"] = "Search and Replace All (RegExp)";
  function filter_searchReplaceAllRegExpConfigure($filter_data)
  {
    print "Search (Regular Expresion):<br />";
    print "<input type='text' name='search' value='".widget_safe($filter_data["search"])."' />";
    widget_errorGet("search");
    print "<br /><br />";
    print "Replace:<br />";
    print "<input type='text' name='replace' value='".widget_safe($filter_data["replace"])."' />";
    widget_errorGet("replace");
  }
  function filter_searchReplaceAllRegExpValidate($filter_data)
  {
    if (!$filter_data["search"])
    {
      widget_errorSet("search","required field");
    }
  }
  function filter_searchReplaceAllRegExpExec($filter_data,$text)
  {
    if (preg_match($filter_data["search"],$text))
    {
      return $filter_data["replace"];
    }
    return $text;
  }

With this in place, add a Search and Replace All (RegExp) filter to the Image URL field, and for Search e.g.

(example1|example2)

...and Replace e.g.

/images/noimage.jpg

And then any Image URL containing "example1" or "example2" will be fully replaced with no image URL...

Cheers,
David.
--
PriceTapestry.com

Submitted by BobL on Mon, 2016-04-04 12:53

Bob L.

Spot on, Mr. David.

Thank you very much for the great support and great software.

Submitted by BobL on Wed, 2016-04-06 12:27

Bob L.

Hi David,

Hope all is well.
I really like this script.
Used it in a few places already where images from feeds were broken.

The problem came when searching for a product name with a broken image, it replaces the product name that contained the text with the /images/noimagejpg.

Example :
Product name - MyBlueWidgets
searchRexExpAll (Blue)
replace (/images/noimage.jpg)

Bob L.

Submitted by support on Wed, 2016-04-06 14:34

Hi Bob,

It sounds like there may be an instance of the filter accidentally associated with the Product Name field instead of the Image URL field - that would be the only way that the product name field would be affected - that should be all it is...

Cheers,
David.
--
PriceTapestry.com