Solved

PHP Replace text in file with synonym from dB

Posted on 2010-08-14
2
1,177 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
2 Comments
 
LVL 3

Accepted Solution

by:
bombwriter earned 500 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
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 count occurrences of each item in an array.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now