aramikgh Posted April 28, 2010 Share Posted April 28, 2010 i know that you can do: str_replace($replaceWith,$replacethis,$sentence); lets say i did that 26 times one for each letter of the alphabet. Can i still do that but once it reaches a charater that is a part of a word that has the "#" symbol in front of it, it wont replace any characters in that string? so i would want this : " this is a #test" to be : "**** ** * #test" it's not going to be all stars but just using it for this purpose. any comments/ideas? thanks!! Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted April 28, 2010 Share Posted April 28, 2010 function replace_chars($text) { echo '<pre>'.print_r($text, true).'</pre>'; return str_repeat('*', strlen($text[0])); } $text = 'this is a #test'; $result = preg_replace_callback('/(?<!#|[a-z])[a-z]+/i', 'replace_chars', $text); echo $result; Quote Link to comment Share on other sites More sharing options...
aramikgh Posted April 29, 2010 Author Share Posted April 29, 2010 That words perfect but what if instead of the "*" i wanted each letter to be different like for every "a" it would replace it with a "a1" and be would be "b1" and etc... so $text = "theres #are the abc" would be "t1h1e1r1e1s1 #are t1h1e1 a1b1c1" ?? sorry guess i should have been more clear - i thought there would be a way you can replace each letter with something different except for the letters attached to the word with the #... function replace_chars($text) { echo '<pre>'.print_r($text, true).'</pre>'; return str_repeat('*', strlen($text[0])); } $text = 'this is a #test'; $result = preg_replace_callback('/(?<!#|[a-z])[a-z]+/i', 'replace_chars', $text); echo $result; Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted April 29, 2010 Share Posted April 29, 2010 Are you wanting all letters to have the same character after them? or each one different? Quote Link to comment Share on other sites More sharing options...
aramikgh Posted April 29, 2010 Author Share Posted April 29, 2010 good questions - i guess i need to think about being more clear, sorry. i would want each letter to have something or even if each letter got replaced with a different set of letters like "a" got replaced with "a1" and "b" got replaced with "493" something like that.. i know i prob would have to write what each letter would be replaced with but how do i avoid a word in the string with a # sign is what im trying to ask. sorry for the confusion but thanks much for your help! Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted April 29, 2010 Share Posted April 29, 2010 This function function replace_chars($text) { echo '<pre>'.print_r($text, true).'</pre>'; return str_repeat('*', strlen($text[0])); } runs on every word that doesn't have a # before it The variable you will need to manipulate is $text[0] Note that the echo is only there to demonstrate what is matched. You can remove that line. Once you've replaced the letters in the string with the ones you want you can return the string, which will then replace what the old word was. I would probably use str_replace myself, passing an array of letters to replace, and the letters to replace them with Quote Link to comment Share on other sites More sharing options...
aramikgh Posted April 29, 2010 Author Share Posted April 29, 2010 perfect - ill play around with that... thanks! This function function replace_chars($text) { echo '<pre>'.print_r($text, true).'</pre>'; return str_repeat('*', strlen($text[0])); } runs on every word that doesn't have a # before it The variable you will need to manipulate is $text[0] Note that the echo is only there to demonstrate what is matched. You can remove that line. Once you've replaced the letters in the string with the ones you want you can return the string, which will then replace what the old word was. I would probably use str_replace myself, passing an array of letters to replace, and the letters to replace them with Quote Link to comment Share on other sites More sharing options...
aramikgh Posted April 29, 2010 Author Share Posted April 29, 2010 K i got what i wanted it to do... thanks!! but now one more question - im sure it's really easy but i just can't get it... how do i select the words with only the # sign rather then everything but... perfect - ill play around with that... thanks! This function function replace_chars($text) { echo '<pre>'.print_r($text, true).'</pre>'; return str_repeat('*', strlen($text[0])); } runs on every word that doesn't have a # before it The variable you will need to manipulate is $text[0] Note that the echo is only there to demonstrate what is matched. You can remove that line. Once you've replaced the letters in the string with the ones you want you can return the string, which will then replace what the old word was. I would probably use str_replace myself, passing an array of letters to replace, and the letters to replace them with Quote Link to comment Share on other sites More sharing options...
litebearer Posted April 29, 2010 Share Posted April 29, 2010 credit goes here james dot randell at hotmail dot co dot uk http://php.net/manual/en/function.in-array.php <?php /** * Takes a needle and haystack (just like in_array()) and does a wildcard search on it's values. * * @param string $string Needle to find * @param array $array Haystack to look through * @result array Returns the elements that the $string was found in */ function find ($string, $array = array ()) { foreach ($array as $key => $value) { unset ($array[$key]); if (strpos($value, $string) !== false) { $array[$key] = $value; } } return $array; } $sentence = "this is a #test of the function. It #finds more than one word."; $needle = "#"; $new_a = find($needle, $array = explode(" ",$sentence)); print_r($new_a); ?> Quote Link to comment Share on other sites More sharing options...
aramikgh Posted April 29, 2010 Author Share Posted April 29, 2010 Would it be possible to do a ... $result = preg_replace_callback('/(?<!#|[a-z])[a-z]+/i', 'replace_chars', $text); that take all the words words with the # at the END and change those strings rather than the #sound being at the begining and not changing those? credit goes here james dot randell at hotmail dot co dot uk http://php.net/manual/en/function.in-array.php <?php /** * Takes a needle and haystack (just like in_array()) and does a wildcard search on it's values. * * @param string $string Needle to find * @param array $array Haystack to look through * @result array Returns the elements that the $string was found in */ function find ($string, $array = array ()) { foreach ($array as $key => $value) { unset ($array[$key]); if (strpos($value, $string) !== false) { $array[$key] = $value; } } return $array; } $sentence = "this is a #test of the function. It #finds more than one word."; $needle = "#"; $new_a = find($needle, $array = explode(" ",$sentence)); print_r($new_a); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.