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.
you can use str_replace or preg_replace or ereg_replace to do what you are looking to do. The easiest is going to be something like this: $string = 'I want to store go'; $newString = str_replace('store go', 'go to the store', $string); PHP: http://www.php.net/str_replace http://www.php.net/ereg_replace http://www.php.net/preg_replace
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:
I'm sorry.. one more question if you are still here. Font color. Can I add a font color to the replaced text?
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.
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; ?>