Despite being a complete php newbie, I took on this challenge at work :D
What I want to do is:
- Take the contents of a textarea form (in this case, a news article)
- Compare it to a simple list of 1000 words in a text file
- If a word in the article matches one in the list, print it as normal; if not, highlight it.
So far, I have...
[PHP]/* Get the form contents */
$article = $_POST["article"];
/* Create array from word list (list from http://www.giwersworld.org/computers/linux/common-words.phtml) */
$fp = fopen('wordlist.txt','r');
if (!$fp) {exit('Error: Unable to open word list.');}
$wordlist = explode("\n", $fp);[/PHP]
But I don't think the array's being created properly...
I think I need to run a foreach loop to do the actual comparing/printing part, but can anyone help me with actual guts of it?
Thanks :)
5 1437
Ok, firstly, you're using fopen and then taking each element and exploding it into a string. Why not just use file_get_contents instead of fopen?
file_get_contents will return a string containing the entire contents of the file.
So start by creating a new array of the words you want to match -
$list = array("hello", "world", "today", "blah");
-
Then loop through that array with a foreach. -
foreach ($list as $word) {
-
str_replace($word, "<span style='background-color: red'>".$word."</span>", $fp);
-
}
-
This should go through each word in your words list and replace any occurences of it with the same word contained in span html tags with a background style set.
Sorry, I jumped into that without reading it properly...
Ok, it should be almost the same code as it's the same idea, but...
you need to close the file afterwards.
fclose($fp);
Now I see you explode the word list, this isn't necessary. You should want to keep the wordlist as an array. This way, you can traverse this array and replace any occurences of the word in your article text, the way I've shown in the code above.
Also, I really recommend reading http://php.net/
The manual and documentation is really good.
Thanks Steve :)
Ok, so now I have:
[PHP] /* Get the form contents */
$rawarticle = $_POST[ "article" ];
/* Create array from word list (list from http://www.giwersworld.org/computers/linux/common-words.phtml) */
$fp = fopen( 'wordlist.txt', 'r' );
if ( !$fp ) { exit( 'Error: Unable to open word list.' ); }
$list = explode( "\n", $fp );
/* Check each word in the article */
foreach ( $list as $word ) {
$fixedarticle = str_replace( $word, "<span style='background-color: red'>".$word."</span>", $article );
}
echo $fixedarticle;
/* Close the word file */
fclose( $fp );[/PHP]
Firstly, I need it to highlight words that *aren't* in the list (it's all about trimming the English down for non-native speakers). So suppose I need to check the $article string for each $word, and add the styling if it's not in the list...
Secondly, now it isn't outputting anything and I'm not entirely sure why...
Ok, I've found an easier way of making the word list array.
[PHP]/* Get the form contents */
$article = stripslashes($_POST[ "article" ]);
$fixedarticle = $article;
/* Create array from word list (list from http://www.giwersworld.org/computers/linux/common-words.phtml) */
$wordlist = array_map( "rtrim", file( "wordlist.txt" ) );
/* Check each word in the article */
foreach ( $wordlist as $word ) {
$fixedarticle = str_ireplace( $word, "<strong>" . $word . "</strong>", $article );
}
echo $fixedarticle;[/PHP]
While I am getting some output now, it's just the original form contents without the new formatting applied. Can anyone tell me why?
i think you need smth like this
[PHP]/* Get the form contents */
$article = stripslashes($_POST[ "article" ]);
$wordlist=file("wordlist.txt" )
$fixedarticle='';
foreach ( $wordlist as $word ) {
if( $word==$article) $fixedarticle. = '<strong>'. $word . '</strong><br />';
else
$fixedarticle = . $word .'<br />' ;
}
echo $fixedarticle;[/PHP]
Sign in to post your reply or Sign up for a free account.
Similar topics
by: matt |
last post by:
I have compiled some code, some written by me, some compiled from
various sources online, and basically i've got a very simple flat file
photo gallery. An upload form, to upload the photos and give...
|
by: Andrew McLean |
last post by:
I have a problem that is suspect isn't unusual and I'm looking to see if
there is any code available to help. I've Googled without success.
Basically, I have two databases containing lists of...
|
by: existatus |
last post by:
In a form there are entries like this:
<input type="text" name="first_name" size="24" />
<input type="text" name="last_name" size="24" />
<input type="hidden" name="must_have" value="12345" />...
|
by: moconno5 |
last post by:
Hello again,
I am still working on my same project and have run into another little problem. I have created a flat file with data from a server, each line looks like this:
BLAT Search Results
...
|
by: deppeler |
last post by:
Can someone look at this for me:
I am trying to set up a script to edit an item in a flat file DB but I don't seem to be getting the data to the Photoedit script. It seems to be reading the 1st line...
|
by: santhoshs |
last post by:
Hello
I am required to parse two files that contain email addresses and figure out a way to get the matching and non-matching email addresses from both the files. I was able to get the matching...
|
by: lxyone |
last post by:
Using a flat file containing table names, fields, values whats the
best way of creating html pages?
I want control over the html pages ie
1. layout
2. what data to show
3. what controls to...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |