imperialized Posted March 2, 2008 Share Posted March 2, 2008 OK, here is my predicament. I have 2 .txt files with information directly correlating with each other. What I am trying to do is write an emoticons script for my news script. I have accomplished this in a very bulky way, I had to type everything out. $replacednews = str_replace("", "<img src=\"emoticons/1.gif\" border=0>", $shortnews); // Face $replacednews = str_replace("", "<img src=\"emoticons/10.gif\" border=0>", $replacednews); // Face $replacednews = str_replace("", "<img src=\"emoticons/11.gif\" border=0>", $replacednews); // Sweating Face Face $replacednews = str_replace("", "<img src=\"emoticons/3.gif\" border=0>", $replacednews); // Sad Face $replacednews = str_replace("", "<img src=\"emoticons/5.gif\" border=0>", $replacednews); // Grin Face $replacednews = str_replace(":greensmoke:", "<img src=\"emoticons/cigarette.gif\" border=0>", $replacednews); // Green smoking Face $replacednews = str_replace(":smokechill:", "<img src=\"emoticons/smokin_chillen.gif\" border=0>", $replacednews); // smoking Face However, in order to make it replace new smiles, I'd have to manually type in the code. The text files contain the smilie text faces in one and the image that relates to which face in the other. Each entry is parallel to the one it relates to. ex: is on line 1 of the smilie.txt and 1.gif is on line 1 of the other text file. So my question is: How do I automate this process of replacing so that I can just edit the text files and it will do all this automatically. Quote Link to comment https://forums.phpfreaks.com/topic/94031-get-data-from-2-files-into-array/ Share on other sites More sharing options...
Chris92 Posted March 2, 2008 Share Posted March 2, 2008 Ok, that's very simple to do: <?php $bbcode = explode("\n", file("code.txt")); $bbreplacements = explode("\n", file("replacments.txt")); str_replace($bbcode, $bbreplacements, $shortnews); ?> Quote Link to comment https://forums.phpfreaks.com/topic/94031-get-data-from-2-files-into-array/#findComment-481718 Share on other sites More sharing options...
wildteen88 Posted March 2, 2008 Share Posted March 2, 2008 Chris92, your code wont work. file() returns the lines of the file being read into an array. So need to use explode. <?php $bbcode = file("code.txt", FILE_IGNORE_NEW_LINES); $bbreplacements = file("replacments.txt", FILE_IGNORE_NEW_LINES); $shortnews = str_replace($bbcode, $bbreplacements, $shortnews); ?> Quote Link to comment https://forums.phpfreaks.com/topic/94031-get-data-from-2-files-into-array/#findComment-481763 Share on other sites More sharing options...
imperialized Posted March 3, 2008 Author Share Posted March 3, 2008 wildteen88, works perfectly. Very simple as well, I should have known Thanks for the help. Much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/94031-get-data-from-2-files-into-array/#findComment-482142 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.