Help with preg_replace() for keywords

Discussion in 'PHP' started by a4kata, Jul 25, 2009.

  1. #1
    Hello, I want to cut the my variable for keywords . Here is my code :

        $replace = array('([\40])','([^a-zA-Z0-9-0-9a-zA-ÿ³¿¸])','(-{2,})');
        $with = array('-','','-');
        $filenameurl = preg_replace($replace,$with,$variable);
    Code (markup):
    This code edit the variable with "-" I wanna to be ", " instead "-" Thanks in advance and sorry for my bad english .
     
    a4kata, Jul 25, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    So basically you're trying to match and replace

    '([\40])' -> "-"
    '([^a-zA-Z0-9-0-9a-zA-ÿ³¿¸])' -> ", " This will basically replace any characters with a ,
    '(-{2,})' -> "-"

    You realize the array is seperate matches right?

    Because if all you want to do is find \40 and replace it with ", ", then you don't even need to use an array, you could simply do preg_replace('([\40])', ", ", $variable);

    Otherwise this might be less processor intensive:
    $filanemurl = str_replace("-", ", ", $variable);
     
    kblessinggr, Jul 25, 2009 IP
    a4kata likes this.
  3. HivelocityDD

    HivelocityDD Peon

    Messages:
    179
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can simply achieve this using

    
    
    $filenameurl = str_replace("-", ", ", $variable);
    
    
    Code (markup):
     
    HivelocityDD, Jul 25, 2009 IP