SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm having a few problems on working out how to do this. (PHP)

    My script gets passed a search varaible which is typed in by the user.

    The script returns something like;

    books+cars+hotrods

    This then needs to be converted to a variable I can uses as a file reference - which can be done by str_replace ("+","/" $variable);

    however: I can't work out how to change the incoming variable to the correct format if the input is something like:

    +books++++++cars+++hotrods++

    Anyone know how I can always convert something like the above to:

    books/cars/hotrods

    Cheers!!














    <Edited by ChilliBoy on 01-12-2001 at 08:18 AM>

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This would be my method, it works fine, but I dont know efficient doing a reg exp grep is..

    $something = explode( "+", $something);
    $something = preg_grep ("/[^\+]/", $something);
    $something = implode ("/", $something);

    This will return a string with "books/cars/hotrods" in..

    Hope this helps.. if not Freddy will probably give you a pointer to the right direction

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for the help. Not that I'm averse to your idea, but is there no way of doing it without calling a regex?

    I don't know how much calling one regex would slow things down (not much I suppose) but on the face of it, it would seem like a very simple operation which could be done without it.

    Its just that I need the script to execute as fast a possible, and I'm therefore looking to cut anything out that may slow things down - however slightly.

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure if there is a non regexp grep in php, but you could do something like..

    $something = explode( "+", $something);
    while( list($key, $val) = each($something)) {
    if ($val) { $newarray[] = $val; }
    }
    $something = implode ("/", $newarray);

    This would get rid of the regexp, but like you say, it does seem like a lot of code to do something fairly simple. Perhaps someone can unearth a nice PHP function by magic

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    May have to delve into the PHP manual. I'd have thought there would be a built in function somewhere, as I guess this is a pretty common need for forms etc.

    Maybe it a combination of two functions which need to be spotted?

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like this way

    $str = "+books++++++cars+++hotrods++";
    $str = implode("/", array_unique(explode("+", $str)));


    will give you

    /books/cars/hotrods
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers Freddy - nice and tidy solution!


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
  •