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

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

4 Comments

right. Any pointers to a slugify() function that utilizes this?
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
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 :(
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?
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.

1 Comment

Unfortunately this doesn't seem to help
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.