Preg_Match Alternative

Discussion in 'PHP' started by glasglow, Jun 25, 2007.

  1. #1
    Right now I am using preg_match to give me this kind of results:


    "I want to store go" Where I preg_match "store go" to echo "I want to go to the store"

    What I want though, is to preg_match inside a sentence and replace only a part of that sentence, the part that was matched, while leaving the rest the same. So I could have this sentence:

    "I want to 'store go' this afternoon"

    Where I would preg_match "store go" and echo it with the replacement:

    "I want to 'go to the store' this afternoon."

    Another function would be ok too as long as I could do what I wanted.. javascript too.. Just looking for a way to do it.
     
    glasglow, Jun 25, 2007 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    jestep, Jun 25, 2007 IP
    glasglow likes this.
  3. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Wonderful.. yes yes.. what I was looking for. Possible to do multiple replaces?
     
    glasglow, Jun 25, 2007 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    You can replace using an array of needles and haystacks.

    
    
    $matchArray = array('store go', 'hi there', 'super market');
    $replaceArray = array('go to the store' , 'hello', 'gas station');
    
    $string = 'hi there, I store go super market';
    
    $newString = str_replace($matchArray,$replaceArray,$string);
    //will output 'hello, I go to the store gas station'
    
    
    PHP:
     
    jestep, Jun 25, 2007 IP
  5. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Man.. Thank you very much. Have a great evening.. or day..
     
    glasglow, Jun 25, 2007 IP
  6. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    I'm sorry.. one more question if you are still here. Font color. Can I add a font color to the replaced text?
     
    glasglow, Jun 25, 2007 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    You could do something like:

    
    
    $replaceArray = array(
    '<span class="blue">go to the store</span>' , 
    '<span style="color:#03f;">hello</span>', 
    'gas station'
    );
    
    
    PHP:
    I would use a span class and then specify in your stylesheet to change the color.
     
    jestep, Jun 25, 2007 IP
  8. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #8
    Rep given.. thanks for the input today.. exactly what I needed.
     
    glasglow, Jun 25, 2007 IP
  9. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #9
    I would like to do this with a mysql database. I tried using a connection and replacing the hard coded results for $matcharray and $replaceArray with $row[rowname] but I am not getting it to loop through all possible. Any suggestions?

    <?
    $matchArray = array
    (
    'word 1',
    'word 2'
    );

    $replaceArray = array
    (
    '<span style="color:#03f;">replacement 1</span>',
    '<span style="color:#03f;">replacement 2</span>'
    );

    $string = $_POST['data'];
    $newstring = str_replace($matchArray,$replaceArray,$string);
    echo "<center><b>Old Text</center></b><br/><hr>";
    echo $_POST['data'];
    echo "<br/><br/>";
    echo "<center><b>New Text</center></b><br/><hr>";
    echo $newstring;
    ?>
     
    glasglow, Jun 29, 2007 IP