1

In a site I'm working on, im converting strings to slugs using the answer in this question. It works, but I'm finding there are HUGE memory leak issues. I've done some research and found that this is just currently a bug in PHP.

Are there any alternatives to accomplish something like strings to slug?

EDIT:

There's another interesting angle to this problem. I'm re-developing a scraper that was made using regex (ugh, i know), so I decided to use DOMDocument / XPath as a solution.

The interesting thing is, the original regex scrape, also uses the above slugify() function, and there are no memory issues. However, once I setup the DOMDocument scrape, the scrape crashes halfway through and the error is always on the preg_replace() line in the slugify() function above.

So despite both scenarios using the exact same slugify() function, only the DOMDocument version crashes on the preg_replace() line

Community
  • 1
  • 1
djt
  • 7,297
  • 11
  • 55
  • 102

3 Answers3

3

Preg_replace is pretty good for this, but an alternative is to hack them out using http://php.net/manual/en/function.str-replace.php

Jared Drake
  • 982
  • 4
  • 12
  • right. Any pointers to a slugify() function that utilizes this? – djt Nov 10 '12 at 05:36
  • ha yeah i know... but I'm doing a scrape (NOT using preg_replace! Use DOM Document) but when a certain text comes in, I have to make it into a slug before going into the DB. So I end up with these loops of preg_replace() and apparently it's just compounding the memory usage each time – djt Nov 10 '12 at 05:55
  • UGH! man I don't know. I still don't have a strong solution for you :/ Have you tried separating your preg_replace out to a function to see if you can kill it? Maybe try some phpUnit on it... I'm sorry man :( – Jared Drake Nov 10 '12 at 06:13
  • Hm, i haven't tried putting the preg_replace() in a separate function. What do you mean by "kill it"? Do you have an example? – djt Nov 11 '12 at 04:20
1

By unsetting the variable, you should be able to free up some memory. Yes it's dirty but might work

static public function slugify($text) {    
  // replace non letter or digits by -   
  $text2 = preg_replace('~[^\\pL\d]+~u', '-', $text);

  // unset $text to free up space
  unset($text);
  // trim   
  $text2 = trim($text2, '-');

  // transliterate   
  $text2 = iconv('utf-8', 'us-ascii//TRANSLIT', $text2);

  // lowercase
  $text2 = strtolower($text2);

  // remove unwanted characters
  $text = preg_replace('~[^-\w]+~', '', $text2);

  // unset $text2 to free up space
  unset($text2);

  if (empty($text))   {
    return 'n-a';   
  }
  return $text; 
}

https://bugs.php.net/bug.php?id=35258&edit=1

http://www.php.net/manual/en/function.preg-replace.php#84285

Hopefully you find a cleaner solution.

aaronott
  • 396
  • 1
  • 6
0

I found this bug https://bugs.php.net/bug.php?id=38728 and it says to use the mb_eregi_replace() function insead.

It worked for me.

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58