Hi,
I have some data to find and replace from a string.
The find and replace data is in a mysql table
so I can easily extract it and build 2 arrays,
or build an associated array
( I am not sure which, so I did both)
$sql = "SELECT word,replacement FROM theos order by word";
$result = mysql_query($sql) or die("Could NOT select words". mysql_error() );
$words = array();
$replacements= array();
$words_reps = array();
$new_words = array();
$new_words_reps = array();
while ($row = mysql_fetch_assoc($result)) {
extract($row);
$words[] = $word;
$replacements[] = $replacement.' ';
$words_reps[$word] = $replacement ;
}
The string to do the find and replace on is a sentence like this:
$input_data =
there is something about being the parent of a very small child a child who has not yet begun to form words that has exerted a subtle pressure on the way i think about language in general my investment in words is heavy and more or less literal in the sense that they are the means by which i make at least in theory my living since my son was born six months ago the first way in which my relationship with words has changed is this i haven't been able to get nearly enough of the words down on paper
Now I could make that array:
$input_data_arr = explode(' ', $input_data);
But I don't know if I need to.
My FIND array $words
looks like this:
2
a
about
access to
across the
advert
advice
ago
agreement
My REPLACE array $replacements
looks like this:
{2|two|only two}
{a|some|a very|a definite|a specific|a particluar}
{about|regarding|concerning|with regards to|in relation to|related to|pertaining to|on the subject of|when it comes to|in regards to|in regard to|with regard to|on the subject off|as regards to}
{access to|usage of|use of|the means to access}
{across the|around the|all over the|over the|through the|round the|all around the}
{advert|advertisement|ad|offer|promotion|special offer|promo|campaign|deal|special promotion|special deal}
{advice|guidance|suggestions|tips|assistance|information|help and advice|recommendations|help|aid|instruction|counsel}
{ago|back|in the past|earlier|previously|previous|prior}
{agreement|arrangement|contract|deal}
AND THE associated array:
( Using this to list it:
foreach($words_reps as $x=>$x_value) {
echo $x . "-->" . $x_value;
echo "<br>";
}
2-->{2|two|only two}
a-->{a|some|a very|a definite|a specific|a particluar}
about-->{about|regarding|concerning|with regards to|in relation to|related to|pertaining to|on the subject of|when it comes to|in regards to|in regard to|with regard to|on the subject off|as regards to}
access to-->{access to|usage of|use of|the means to access}
across the-->{across the|around the|all over the|over the|through the|round the|all around the}
advert-->{advert|advertisement|ad|offer|promotion|special offer|promo|campaign|deal|special promotion|special deal}
advice-->{advice|guidance|suggestions|tips|assistance|information|help and advice|recommendations|help|aid|instruction|counsel}
ago-->{ago|back|in the past|earlier|previously|previous|prior}
agreement-->{agreement|arrangement|contract|deal}
In order to use preg_replace(),
I wanted to add some reg_exp so that when I search for
"a" it does NOT get found in "about" but only as "a"
( maybe I should use a \b for word boundary ? )
Anyway, I changed the FIND array to be:
$new_words
"/2$/"
"/a$/"
"/about$/"
"/access to$/"
"/across the$/"
"/advert$/"
"/advice$/"
"/ago$/"
"/agreement$/"
I also change the associated array:
$new_words_reps:
"/2$/"-->{2|two|only two}
"/a$/"-->{a|some|a very|a definite|a specific|a particluar}
"/about$/"-->{about|regarding|concerning|with regards to|in relation to|related to|pertaining to|on the subject of|when it comes to|in regards to|in regard to|with regard to|on the subject off|as regards to}
"/access to$/"-->{access to|usage of|use of|the means to access}
"/across the$/"-->{across the|around the|all over the|over the|through the|round the|all around the}
"/advert$/"-->{advert|advertisement|ad|offer|promotion|special offer|promo|campaign|deal|special promotion|special deal}
"/advice$/"-->{advice|guidance|suggestions|tips|assistance|information|help and advice|recommendations|help|aid|instruction|counsel}
"/ago$/"-->{ago|back|in the past|earlier|previously|previous|prior}
"/agreement$/"-->{agreement|arrangement|contract|deal}
OK !
So now I want to use the preg_replace()
BUT my question is - do I need to use the associated array
or do I use the two FIND and REPLACE arrays
Since these 2 arrays were generated from the same table, they have the same
natural keys - does that keep them in step when replacing ?
If I use the two arrays, I guess it should like this ...
$new_data = preg_replace($new_words,$replacements,$input_data);
If is the assocated array, I don't know how to write that !! :o
Am I correct in thinking that the input can be a string ?
Or does that need to be an array as well ?
Sorry,
A lot of questions .. but I am not sure how to do this !! 
Thanks for any help !
.