1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

preg_replace - help needed

Discussion in 'PHP' started by ruby, Nov 4, 2007.

  1. #1
    A few of you regular expression gurus should find this easy....

    Basically I have a string and I want to strip out anything thats not alphanumeric, and i want to replace any spaces with a -

    So for example this string: t2es% no* *ss3aa

    would become: t2es-no-ss3aa

    If anyone could write the preg_replace to do that Id appreciate it!
     
    ruby, Nov 4, 2007 IP
  2. bobb1589

    bobb1589 Peon

    Messages:
    289
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    function replace($str){
    
    $string_array = str_split($str);
    
    $count = 0;
    
    foreach($string_array as $char){
    	if(!preg_match("([a-zA-Z0-9_-]+)",$char)){
    		$string_array[$count]="-";
    	}
    	$count++;
    }
    
    $check = 0;
    foreach($string_array as $checkchar){
     if($checkchar=="-"){
    	if($checkchar == $string_array[$check+1]||$checkchar == $string_array[$check-1]){
    		unset($string_array[$check]);
    	}
    	$check++;
    	}else{
    		$check++;
    	}
    }
    $string = implode("",$string_array);
    echo $string;
    }
    ?>
    PHP:
    this works... probably a little heavy... but it works haha
     
    bobb1589, Nov 4, 2007 IP
  3. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Try this:
    $newString = preg_replace('/\s/', '-', preg_replace(array('/[^a-z0-9\s]/i'), array(''), $originalString));
    PHP:
     
    phper, Nov 4, 2007 IP
    Colleen and ruby like this.
  4. ruby

    ruby Well-Known Member

    Messages:
    1,854
    Likes Received:
    40
    Best Answers:
    1
    Trophy Points:
    125
    #4
    Cool thanks! Ill try and let you know!
     
    ruby, Nov 4, 2007 IP
  5. ruby

    ruby Well-Known Member

    Messages:
    1,854
    Likes Received:
    40
    Best Answers:
    1
    Trophy Points:
    125
    #5
    That worked a treat phper.... +rep for you!
     
    ruby, Nov 4, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    The idea of using arrays is good, but rather pointless the way you do it.

    I'd do it this way:
    
    $text = preg_replace(array('~\s+~', '~[^a-z0-9-]~i'), array('-', ''), $text);
    
    PHP:
     
    nico_swd, Nov 5, 2007 IP
    Colleen likes this.
  7. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #7
    nico_swd:
    Thanks for the correction, nico_swd. I was initially gonna suggest the same way like you posted above, but then realised that if the original string had a dash ('-') character in it, it wouldn't be removed (while it is a non-alphanumeric character).
    e.g.: "x%- g" would become "x--g" instead of "x-g"

    After I updated it then I forgot to take the value out of the array :)


    ruby:
    Thanks for the rep :)

    Regarding nico_swd's correction above, it wouldn't affect the result at all if you just keep your code like what you have now. But if you are really keen, below is a cleaner code:
    $newString = preg_replace('/\s/', '-', preg_replace('/[^a-z0-9\s]/i', '', $originalString));
    PHP:
     
    phper, Nov 6, 2007 IP