1

I want to replace some string in searchword.txt file with replaceword.txt file, but with my php code it can only replace the first two strings, i want to replace all the strings in the file.

here is my php code:

$txt_s = file_get_contents("searchword.txt");
$txt_s = explode(";", $txt_s);
$txt_r = file_get_contents("replaceword.txt");
$txt_r = explode(";", $txt_r);
$term = str_replace($txt_s, $txt_r, $last_tmp_);

here is how my searchword.txt file looks like:

hello; modern; corn; banana; apple;

here is how my replaceword.txt file looks like:

goodbye; fashionable; popcorn; monkey; sweet;

how can i replace all the strings, not just first two strings? or may be any different way to do this please let me know.

1
  • i tested your code and it works well on my end. try str_ireplace for insensitive case . see here Commented May 3, 2013 at 20:39

1 Answer 1

2

You have to strip whitespaces for example with array_map()

$newarray = array_map('trim', $array);

Use:

$txt_s = file_get_contents("searchword.txt");
$txt_s = array_map( 'trim', explode(";", $txt_s) );
$txt_r = file_get_contents("replaceword.txt");
$txt_r = array_map( 'trim', explode(";", $txt_r) );
$term = str_replace($txt_s, $txt_r, $last_tmp_);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.