Solved

PHP Replace text in file with synonym from dB

Posted on 2010-08-14
2
1,184 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 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

10 Experts available now in Live!

Get 1:1 Help Now