Creating a search function on my site using php/sql, simple enough - just using a SELECT ALL query on the database using the LIKE clause and echoing the result on the page. My question is, how can I add spelling suggestions in case the user mistyped their search query. Mysql doesn't return anything unless the search term matches exactly with the database content, e.g. "Dofs" will not return "Dogs". So how can spelling suggestions be added?
Thanks.
Following you will find an excellent article by Peter Norvig on how to write a spell checker:
http://www.norvig.com/spell-correct.html
and the following two links are implementations in PHP of the code found in the article:
http://www.phpclasses.org/browse/package/4859.html
http://soundofemotion.com/spellcorrect.txt
Hope this helps.
What about PHP's pspell extension?
<?php
$pspell_link = pspell_new("en");
if (!pspell_check($pspell_link, "dofs")) {
$suggestions = pspell_suggest($pspell_link, "dofs");
foreach ($suggestions as $suggestion) {
echo "Possible spelling: $suggestion<br />";
}
}
?>
This PHP extension requires that you have aspell libraries installed.
You need to check out something like the following:
http://phpir.com/spelling-correction
You will need a dictionary and the levenstien function basically.
in addition to Joe's excellent solution, you can make a soap call to provide alternate spellings (based on a search engine's language corpus)
Yahoo Spelling Suggestion:
http://developer.yahoo.com/search/web/V1/spellingSuggestion.html
Google spelling request:
http://code.google.com/apis/soapsearch/reference.html#1_3
Coming here in March 2020, then https://tigitz.github.io/php-spellchecker/ is worth looking at.
From the docs:
PHP-Spellchecker is a spellchecker abstraction library for PHP. By providing a unified interface for many different spellcheckers, you’re able to swap out spellcheckers without extensive rewrites.
Using PHP-Spellchecker can eliminate vendor-lock in, reduce technical debt, and improve the testability of your code.
Related
Is it possible to find the data about a built in method in PHP via code?
For example, we have array_key_exists() which is an internal method.
I want to find out the parameters in this function programatically. The reason is, there is an up coming interview, and I will have to write code on Notepad. There will not be any internet connection to see PHP documentation.
If I can get the information about built in methods via code, it will be really helpful.
Is it at all possible to print meta data of a function? I am not asking about user defined functions, but about PHP's built in functions.
Thanks a lot.
You are looking for Reflection i think:
$refFunc = new ReflectionFunction('preg_replace');
foreach( $refFunc->getParameters() as $param ){
print $param;
}
http://php.net/manual/de/class.reflectionfunction.php
I started learning this very nice PHP ORM api last week: http://phpdatamapper.com/
and have been trying to get up to speed with it
What I'm not seeing in the site documentation, is how to iterate with "$postMapper->all()"?
http://phpdatamapper.com/documentation/usage/finders/
When I try to iterate through the value returned from ->all(), it seems only to have gotten the last element in the table.
Here's the code I have:
// $postMapper uses phpDataMapper framework. It works to create the schema & insert values
$postEntities = $postMapper->all();
$postEntities->execute(); // tried adding this to help things
foreach ( $postEntities as $postEntity);
{
echo $postEntity->title;
echo "<br/>";
}
I see other folks are forking it from GitHub and using it in their projects, so I believe I'm making some mistake in my call logic.
It would be great if someone could share a small example of how to access the query data correctly when using mapper->all()? This is an important part of a PHP stack and I would very much like to be able to use this particular solution in my projects going forward. Thanks
Ok, this turned out to be a "not so smart" mistake on my part. The font in my IDE was too small I guess, and I didn't see the ';' at the end of the line that declares the for loop.
So the ORM is all good, just my code wasn't. I'm updating this in case someone else can learn from my mistake - other option is to just delete the post all together (... not sure which is better)
I am looking at someones project and keep getting this line:
$resource_folder = getScenarioResourceFolder(getScenarioPath($scenario));
I cannot find any function that he implemented under those two names - getScenarioResourceFolder and getScenarioPath.
I started wandering that maybe the name Scenario has something to do with $scenario variable being in those functions. I know it might sound dumb, but I do not know what else to think.
Does anyone know about these function?
I know these:
You can search the function:
http://id1.php.net/manual-lookup.php?pattern=getScenarioPath if no
result, That is must be user-defined function.
You can check yourself by using
if (function_exists('getScenarioPath')) {
...
} else {
...
}
These are clearly custom functions that have been written in.
The simplest solution would be to GREP your entire system for getScenarioResourceFolder - you are looking for a .php file.
If you can't grep or don't know how, then it's time to go digging. Open any PHP files that are related to that project and look for getScenarioResourceFolder().
If you really don't have it, then you'll have to get in touch with the original architect of the project.
I am developing a multi-langual website. Language variables (phrases/word translations) will be entered in a specific file (one different file for each language)
I wanted to know the best way to enter the phrases/word translations, should I use a normal array?
e.g
Filename = English.php
<?php
$translations = array();
$translations['phrase1'] = "this";
$translations['phrase2'] = "that";
..
?>
and in the template file
<?php
include("English.php");
echo $translations['phrase1'];
etc...
I am pretty new to PHP so I am just looking for the best way to do it.
Any suggestions?
Thank you for your help!
There are multiple of ways to do this, the two things that pop-off my head right now are:
1) Have a look at gettext & GNU gettext page. An example implementation of this to look at is Aur Website of ArchLinux. They their app support multiple languages & its all dynamic. user can can switch between languages easily. The source code is available here, study it & see how they did it.
2) Other option could be use a framework like cakephp, as most of these frameworks have translations support
Hope it somewhat helps
I have been looking online for a tutorial to build a template engine. I know there are many engines that exist, like smarty, twig, and pattemplate, that could do exactly what I want, but I am looking to learn how to build one. I started with a template engine that added strings to an array and then displayed the array. Since then I built one using eval() (see below).
<// Define links & folders
define("ROOT_HTTP", "http://" . $_SERVER['HTTP_HOST'] . "/preprocessor");
define("TEMPLATE", "/template");
// Get the template file
$template = file_get_contents("template/template.php");
// Replace
$template = str_replace("<x Title x>", displayTitle(), $template);
$template = str_replace("<x Menu x>", displayMenu(), $template);
$template = str_replace("<x Content x>", displayContent(), $template);
$result = #eval("?>" . $template . "<?");
function displayMenu(){
return "Link1<br />" .
"Link2<br />" .
"Link3<br />";
}
function displayTitle(){
return "Site Title <?php echo date(\"m-d-y\", time()); ?>";
}
function displayContent(){
return file_get_contents("content.php");
}
It works fairly well but its not what I am looking to achieve. I would like to build something that is like the Joomla template with tags like <jdoc:include type="component" />. I would also like it to be able to handle errors inline meaning that it will display the line number of an error or when I call echo "text" it displays text in the correct position inside the template.
How do I create something along those lines?
http://www.phptal.org/ sounds very similar and has good code organization. if extension of mentioned system does not suit the needs, it would at least work as good tutorial
First of all: Immediately forget the idea about using a TE with XML-like tags. Really, it may look nice on the first glance but only causes too much work in the end and is really limiting.
Secondly I obviously recommend you to use Twig. It is clean, fast, extensible and offers all the features you need.
And lastly: I have written a small tutorial how to write a simple but powerful TE in another Stackoverflow question. It is really simple but for smaller projects it may suffice.
I cannot agree with NikiC's point of view.
XML is, although an old syntax, very powerful and brings a lot of advantages -- one of which is its similitude with properly written HTML.
There is nothing limiting in using an XML-based template syntax.
Besides, although Twig is, indeed, an excellent and famous project, it still lacks from a really good separation paradigm. It is still too dangerous and too easy to make mistakes from within the template and cause damages to the application as a whole.
Finally, the best template engine -- just as the best MVC framework -- is the one you feel really comfortable with.
I recommend having a look at FigDice]1, which was inspired by PHPTal, but takes things a few steps further, with an exclusive approach by giving the Web Designer (integrator, html-ist, etc.) a central position with the project -- much more flexible than the Twig-like approach.
I would be happy to read some feedback.
Thanks