?
Solved

PHP Replace text in file with synonym from dB

Posted on 2010-08-14
2
Medium Priority
?
1,256 Views
Last Modified: 2013-12-13
I am trying to PHP replace ANY & ALL word and phrase matches in a text file with associated synonyms from a dB and I had it doing single replaces ok but now I want multiple replaces I can't get it to replace at all :(

In the dB is a table with two fields per record:

seed         :  This is the match element to compare against and find occurences of in the text file.
synonym  :  This is a the partner field of the row conmtaining possible synonyms divided by a pipe | (random selection required)

example seed                 :   "100%"
example replacement     :  "100 per cent"
example field data text   :  "100 percent|one hundred pc|100pc"

The text file with the text to search and replace over is called doc.txt

My code (not working) so far is:

mysql_connect("localhost","wuser","p!as1@h");
mysql_select_db("words");

		$str=implode("\n",file('doc.txt'));
		$fp=fopen('doc.txt','w');

		$results = mysql_query("select * from synonyms");
		while ($row = mysql_fetch_assoc($results)) {

        $seed = $row[seed];
        $synonym = $row[synonym];

		$words = explode("|", $synonym);
		$index = rand(0, count($words)-1);
		$choice = $words[$index];

		while(strpos($str, '/$seed/') !== false) {
     	echo "Replacing $seed with $choice<br>";

		$str = str_replace($seed, $choice,$str);
		}
	}
    fwrite($fp,$str,strlen($str));

Open in new window


Could someone help this novice and show me what I need to do to make it check every line of the doc to see if there is any matches that should be replaced by synonyms?

Thanks
0
Comment
Question by:EE-Member
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
bombwriter earned 2000 total points
ID: 33439848
firs of all you can use $str=file_get_contents('doc.txt'); instead $str=implode("\n",file('doc.txt')); to do the same thing.
In your code you use the strpos function with slashes. You don't need to put, try
    while(strpos($str, '$seed') !== false) {
in line 17.
And finally, to save the file use file_put_contents($filename  , $data)

            
0
 

Author Comment

by:EE-Member
ID: 33443740
Strange I kind of had that before and it would not work but now it does so thanks :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question