Solved

PHP Replace text in file with synonym from dB

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

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
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

724 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