开发者

Remove special chars from URL

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开发者_JS百科 some help.

Cheers


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)


try this,

<?php
$url = 'http%3A%2F%2Fdemo.com';
$decodedurl= urldecode($url);
echo $decodedurl;
?


$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


Try this:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜