SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast toytron's Avatar
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg replace to complicated?

    I'm trying to remove spaces from a string
    Ths string could be
    "function1 function2(x parameters seperated with a whitespace) function3 ..."
    expl. function1 function2(par1 par2 par3) function3 ...

    I don't know how many parameters will be called.

    This is what I have at the moment:
    /([\'||\"][a-zA-Z0-9]*)([[:space:]])([a-zA-Z0-9]*)[\'||\"]/

    It works fine for two parameters (one space) but not for more.
    How would I make this work for x parameters. I'm currently running into problems when I start using (). My $1,$2,$3 get wrong values.

    Also is using regular expressions faster than str_replace?


    thnx

  2. #2
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you tell us what the desired output is?

    And no, regexes are not faster than str_replace. In fact they are way slower in comparison to str_replace. Comparing them doesn't make sense though, since str_replace() matches (sub)strings whereas regexes match patterns.

  3. #3
    SitePoint Enthusiast toytron's Avatar
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to make it like
    function1 function2(par1^par2^par3) function3
    so I can use Explode on the string. When I need the data then it's just a mater of str_replaceing the ^
    par1,par2,par3 can be [_a-zA-Z0-9]

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm still confused. Do you want to extract whatever is in the brackets? Ie. italicised text in the example below?

    Code:
    function1 function2(abc, def, ghi) function3

  5. #5
    SitePoint Enthusiast toytron's Avatar
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to alter
    "function1 function2('abc def ghi') function3"
    into
    "function1 function2('abc^def^ghi') function3"

  6. #6
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $in 'function1 function2(\'abc def ghi\') function3 function4(\'123 456 789\') function5';

    echo 
    preg_replace_callback('/\(\'.+? .+?\'\)/'create_function('$m''return str_replace(" ", "^", $m[0]);'), $in); 

  7. #7
    SitePoint Enthusiast toytron's Avatar
    Join Date
    Nov 2006
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thnx decowski. Does exactly what I want
    Think I understand what you are doing here. Didn't know that stuff was possible.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •