acccc Posted March 24, 2011 Share Posted March 24, 2011 Hello guys this is my first post. Just a quick problem I've encountered, I'm trying to str_replace an array and string together and wondering how it can be done? $text = "this is @value1 and this is @value2"; $array1 = array ( 0 => 'value1', 1 => 'value2'); // just an example $at_replace = str_replace (" . @ " $array1, " . # " $array1, $text); So im basically trying to replace all instances in $text where each word in $array1 starting with '@' will be replaced with a '#'. Also will this be better done with a loop? Any help or direction is greatly appreciated. Quote Link to comment Share on other sites More sharing options...
acccc Posted March 24, 2011 Author Share Posted March 24, 2011 Anybody have any alternatives to solving this problem Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 24, 2011 Share Posted March 24, 2011 You're explanation doesn't make a whole lot of sense. Please show examples (both before & after). Ken Quote Link to comment Share on other sites More sharing options...
matthew9090 Posted March 24, 2011 Share Posted March 24, 2011 you could use the implode function to turn it into string then str_replace then explode into array again. e.g <?php $array = //array implode(" " . $array); str_replace($replace, $array); explode(" " . $array); ?> it should work, just wrote it of the top of my head. Quote Link to comment Share on other sites More sharing options...
acccc Posted March 24, 2011 Author Share Posted March 24, 2011 @matthew, it doesnt seem to do the trick. As an example of what I want to do, your name 'matthew' would be in the array $array1 You post a comment and you write "@john, how are you?" Then the str_replace would replace where '@' and 'john' is (if john is in the array $array1) with a hyperlink to johns profile, similar to what you get in twitter hash tags, youtube comments or facebook. I hope this makes sense. Quote Link to comment Share on other sites More sharing options...
acccc Posted March 24, 2011 Author Share Posted March 24, 2011 I was thinking using the array_walk function, a foreach loop, implode/explode method, or a way to concatenate the str_replace may work? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 24, 2011 Share Posted March 24, 2011 Do only want to replace the '@' at the beginning of the words? So This is @test@ would become This is #test@ or do you want to replace all the '@' symbols in a string, so the test string becomes This is #test# If it's the first variant, you can do something like: <?php function at_to_hash($str) { return (ltrim(str_replace(' @',' #',' ' . $str))); } // //tests // echo at_to_hash('this is @test') . "<br>\n"; // result: this is #test echo at_to_hash('@this@') . "<br>\n"; // result: #this@ echo at_to_hash('@this is @a test') . "<br>\n"; // result: #this is #a test ?> Ken Quote Link to comment Share on other sites More sharing options...
acccc Posted March 24, 2011 Author Share Posted March 24, 2011 Do only want to replace the '@' at the beginning of the words? So This is @test@ would become This is #test@ or do you want to replace all the '@' symbols in a string, so the test string becomes This is #test# If it's the first variant, you can do something like: <?php function at_to_hash($str) { return (ltrim(str_replace(' @',' #',' ' . $str))); } // //tests // echo at_to_hash('this is @test') . "<br>\n"; // result: this is #test echo at_to_hash('@this@') . "<br>\n"; // result: #this@ echo at_to_hash('@this is @a test') . "<br>\n"; // result: #this is #a test ?> Ken Thanks for the speed reply Ken, it worked well and replaces all the @ into # I wanted it to replace the '@'s with '<a href='example.php?user=' so as to make the area where @ is present a link to the user profile. I was wondering now, how to add ''>@username</a>' at the end of the word so as to complete the hyperlink. (Think of what you see in youtube's new commenting system, or facebook's '@' function. This is what im trying to achieve. Ive been trying different things but it doesn't seem to work (the easiest way looked to be to concatenate the str_replace e.g. str_replace ("@" . $array1, "<a href='example.php?user=" . $array1 . "'>@username</a>", $text) but I dont think this works for arrays and im not 100% sure on how to concatenate correctly. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 24, 2011 Share Posted March 24, 2011 For that you probably have to use preg_replace(). Since I rarely use it, I can't give you any help. Ken Quote Link to comment Share on other sites More sharing options...
acccc Posted March 24, 2011 Author Share Posted March 24, 2011 For that you probably have to use preg_replace(). Since I rarely use it, I can't give you any help. Ken Thanks for the point in the right direction, greatly appreciated 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.