views:

50

answers:

3

Hi,

I have a product database and I am displaying trying to display them as clean URLs, below is example product names:

PAUL MITCHELL FOAMING POMADE (150ml)
American Crew Classic Gents Pomade 85g
Tigi Catwalk Texturizing Pomade 50ml

What I need to do is display like below in the URL structure:

www.example.com/products/paul-mitchell-foaming-gel(150ml)

The problem I have is I want to do the following:

1.  Remove anything inside parentheses (and the parentheses)
2.  Remove any numbers next to g or ml e.g. 400ml, 10g etc...

I have been banging my head trying different string replaces but cant get it right, I would really appreciate some help.

Cheers

+2  A: 
function makeFriendly($string)
{
    $string = strtolower(trim($string));
    $string = str_replace("'", '', $string);
    $string = preg_replace('#[^a-z\-]+#', '_', $string);
    $string = preg_replace('#_{2,}#', '_', $string);
    $string = preg_replace('#_-_#', '-', $string);
    return preg_replace('#(^_+|_+$)#D', '', $string);
}

this function helps you for cleaning url. (also cleans numbers)

Osman Üngür
why change it to lower case and then check for `A-Z` - also, instead of that last `preg_replace` you could use `trim($string, '_')`
nickf
ups. i forgot. i'd edited. John, you can also strip "ml" "gr" with str or preg replace.
Osman Üngür
Cheer Osman, few issues with it:1. I need to remove any char in the braquets not just numbers2. Also I need to leave numbers in there which are not connected to "ml" or "gr"Cheers.
John Jones
this code<code>$str = 'Osman Ungur (34000, Turkey)';echo preg_replace('(\(.*\))Us', "", $str);</code>is stripping braquets from string. you can add this to my function. im looking for your second request.
Osman Üngür
Osman,Lovely everything in the braquets has now removed, still problem of "ml" and "g" left, I cant do a str replace becuase it will remove any "g's" from the URL I need it to only remove "g" or "ml" when next to number. Also need to beable to display numbers which are not next to "ml" of "g"
John Jones
+1  A: 
$from = array('/\(|\)/','/\d+ml|\d+g/','/\s+/');
$to = array('','','-');

$sample = 'PAUL MITCHELL FOAMING POMADE (150ml)';
$sample = strtolower(trim(preg_replace($from,$to,$sample),'-'));
echo $sample; // prints paul-mitchell-foaming-pomade
codaddict
A: 

Try this:

trim(preg_replace('/\s\s+/', ' ', preg_replace("/(?:\(.*?\)|\d+\s*(?:g|ml))/", "", $input)));

// "abc (def) 50g 500 ml 3m(ghi)" --> "abc 3m"
nickf