SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2006
    Location
    Gothenburg, Sweden
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace whitespace with a hyphen

    I'm an idiot when it comes to regular expressions. How would i replace whitespace in this kind of array with a hyphen instead?

    PHP Code:
    Array
    (
        [
    0] => nokia
        
    [1] => future mobile
        
    [2] => nokia inc

    I.e [1] => future mobile would be [1] => future-mobile

    What I've come up with so far is this (but it doesn't work):

    PHP Code:
    preg_replace('/\s+/''-'$tagsArray); 

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    $tagsArray=preg_replace('/\s+/', '-', $tagsArray);
    Saul

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2006
    Location
    Gothenburg, Sweden
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah thanks alot. This is definately confusing :P I didn't know you had to assign it back to the same var as your modifying.

    I.e with array_walk() you don't need to assign it back to the var. Like this:

    PHP Code:
    array_walk($tagsArray'trim_value'); //remove whitespace from array    
    $tagsArray preg_replace('/\s+/''-'$tagsArray); //replace whitespace between two word tags with a hyphen 

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You don't necessarily have to assign it back to the same variable, it can be anything -- preg_replace does not modify the original variable, but rather returns the result; while array_walk works on the original array.
    Saul

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2006
    Location
    Gothenburg, Sweden
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well hmm, but you have to assign preg_replace to some variable though? because when I used the function without assigning it to a variable it didn't work.

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, of course, that's what I meant, any variable.
    Saul

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    <?php
     
    header('content-type: text/plain');
     
    $a = array('apples & peaches', 'double  spaces  in  here', "Lets try \n a newline", "a \t tab?");
    $a = array_map(create_function('$s', 'return preg_replace("/\s+/", "-", trim($s));'), $a);
     
    print_r($a);
    :3

    Code:
    Array
    (
        [0] => apples-&-peaches
        [1] => double-spaces-in-here
        [2] => Lets-try-a-newline
        [3] => a-tab?
    )
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simply:
    PHP Code:
    $newArray preg_replace('/\s+/''-'$oldArray); 
    preg_replace works with arrays.


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
  •