Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2004
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    replacing text problem

    Hello

    I have an $output varaiable containing this

    <email@abramsandsmith.com> to: adugarte@my.com (windows-1250)
    <email@aberfeldy.freeserve.co.uk> to: macosta@my.com (31 Jan 2007 16:14)
    <email@bluewavecomputing.com> to: adugarte@my.com (31 Jan 2007 0060)
    <email@aahanet.org> to: jguillen@my.com (31 Jan 2007 16:15:47 -0540)


    I am trying to replace all spaces between () with an underscore _
    How to do that I am going mad trying and trying.

    I am trying to obtain this
    echo $output should return this

    <email@abramsandsmith.com> to: adugarte@my.com (windows-1250)
    <email@aberfeldy.freeserve.co.uk> to: macosta@my.com (31_Jan_ 2007_16:14)
    <email@bluewavecomputing.com> to: adugarte@my.com (31_Jan_2007_0060)
    <email@aahanet.org> to: jguillen@my.com (31_Jan 2007_16:15:47_-0540)


    Any help ? Thanks

  • #2
    Regular Coder
    Join Date
    Oct 2006
    Location
    Bristol
    Posts
    128
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi, I made this bit of code ages ago to display certain parts of a page as php code. There may be a better way of doing it, but this is one...

    PHP Code:
    <?php 
    $output 
    '<email@abramsandsmith.com> to: adugarte@my.com (windows-1250)
    <email@aberfeldy.freeserve.co.uk> to: macosta@my.com (31 Jan 2007 16:14)
    <email@bluewavecomputing.com> to: adugarte@my.com (31 Jan 2007 0060)
    <email@aahanet.org> to: jguillen@my.com (31 Jan 2007 16:15:47 -0540)'
    ;

        
    $beg '(';
        
    $end ')';
        
    $string $output;
        
        
            
    $newstring str_replace($end$beg$string);
            
    $piece explode($beg$newstring);    
            
    $total count($piece);
                        
            for(
    $i=1$i <= $total$i += 2){ //gets every other part
                
    $up = ($i 1);
                
                
    //Change spaces to underscores
                
    if($piece[$i] != ""){ //checks that piece isn't empty
                
                    
    $replace_spaces str_replace(' ''_'$piece[$i]);
                        
    $piece[$i] = '('.$replace_spaces.')';
                        
                        
    $piece[$up];
                }
            }


        foreach(
    $piece as $key=>$output){
            
            if(
    $key %== 0){
                echo 
    "$output";
            }else{
                echo 
    "$output<br/>";
            }
            
        }
        
            
    ?>

    Alex

    P.S apologies for the poor commenting. If you need extra explanation PM me
    Last edited by Alex!; 01-31-2007 at 06:42 PM.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    My solution below is sort of my own "hack"...
    I'm sort of new at regex, so someone can improve on my answer ...

    PHP Code:
    <?php

    $string
    ="<email@abramsandsmith.com> to: adugarte@my.com (31 Jan 2007 16:14)";

    //pattern for the preg_match ... everything between and including ()
    $pattern "/\(([^`]*?)\)/";

    //do the match
    preg_match ($pattern$string$match);

    //replace all spaces with underscores
    $replacement str_replace(" ""_"$match[0]);

    //put the new (contents) back into the string.
    $output preg_replace($match[0], $replacement$string);

    //clean-up the double ((  and ))
    $output str_replace("((""("$output);
    $output str_replace("))"")"$output);

    echo 
    $output;

    ?>

  • #4
    Regular Coder
    Join Date
    Oct 2006
    Location
    Bristol
    Posts
    128
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yup that looks easier and neater xD does it work for the whole output though?

    Alex
    Last edited by Alex!; 01-31-2007 at 05:53 PM.

  • #5
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    another quick way:

    PHP Code:
    while (preg_match('/\(.*?/s.*?\)'$output){ // not sure if its \s or /s
        
    preg_replace('/\((.*?)/s(.*?)\)''($1_$2)'$output);


  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Why not this:

    PHP Code:
    function stripSpaces($matches) {
        return 
    str_replace(' ''_'$matches[1]);
    }

    // get anything between parenthesis
    preg_replace_callback('/\(([^\)]*?)/''stripSpaces'$string); 

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,471
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    ahh... I forgot all about that! Good one!

    http://us2.php.net/preg_replace_callback


  •  

    Posting Permissions

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