[PHP] Re: Question about string replace -PHP
I didn't ask the question about this, but I have a related issue. I'm trying to do a replace, and am using str_replace, but the outcome is not what I expected. Here's my code: ? $fp = fopen(new_data.csv, r); $text = fopen(text.txt, r); $merged = fopen(merged_text.html, w); $filecontents = fread($text,filesize(text.txt)); while ($data = fgetcsv ($fp, 1000, ,)) { $num = count ($data); $fullname = $data[0] . ' ' . $data[1]; $newfullname = '123 ' . $fullname . ' 456'; $filecontents = str_replace($fullname, $newfullname, $filecontents); } fclose ($fp); fwrite($merged,$filecontents); fclose ($merged); fclose ($text); ? Here's the text file it's working on: John Smith br br Peter Cooper br br Mike Brown br br Lisa Crow br Here's the outcome: 123 123 John Smith 456 456 br br 123 123 Peter Cooper 456 456 br br 123 123 Mike Brown 456 456 br br 123 123 Lisa Crow 456 456 br I only want it to have one set of 123 and 456 on either side. Any ideas? Josh -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Question about string replace -PHP
On Wednesday 15 May 2002 05:20, Josh Valerie McCormack wrote: I didn't ask the question about this, but I have a related issue. I'm trying to do a replace, and am using str_replace, but the outcome is not what I expected. Here's my code: ? $fp = fopen(new_data.csv, r); $text = fopen(text.txt, r); $merged = fopen(merged_text.html, w); $filecontents = fread($text,filesize(text.txt)); while ($data = fgetcsv ($fp, 1000, ,)) { $num = count ($data); $fullname = $data[0] . ' ' . $data[1]; $newfullname = '123 ' . $fullname . ' 456'; Remove the above line, it's superfluous. $filecontents = str_replace($fullname, 123 $fullname 456, $filecontents); [snip] I only want it to have one set of 123 and 456 on either side. Any ideas? -- Jason Wong - Gremlins Associates - www.gremlins.com.hk Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* There is no distinctly American criminal class except Congress. -- Mark Twain */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Question about string replace -PHP
Replacing a constant string by another constant string works easily with str_replace (http://www.php.net/str_replace). More advanced replacements (with dynamic parts for example) can be replace using Regular expressions (look that up in the Manual, it's quite complex). Could give an example of what you want to repalce? Dan McCullough wrote: I have a script pulling in certain text from a site. I want the script to then from that retrieved text, to replace the incorrect domain with another domain. What is a way that I can do that, I know the domain that its going to put in the script, and I know the domain that I want to point it to. Any help .. = dan mccullough Theres no such thing as a problem unless the servers are on fire! __ Do You Yahoo!? LAUNCH - Your Yahoo! Music Experience http://launch.yahoo.com -- Bram v. Leur The Netherlands Proud to PHP! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php