How to get difference between two words in php - php

I have a search engine for my website. I have installed a feature like Google's "Did you mean xxx?". It returns corrected string. So, is there a way so that I can compare both of those incorrect and correct strings and bold out the corrected words like Google do?
Here is my code:
<?php
function suggestion($word){
$first_word=substr($word,0,1);
$first_word=strtoupper($first_word);
$query="SELECT * FROM words WHERE UPPER(LEFT(name,1))='$first_word'";
$db_words=mysql_query($query);
$flag=true;
while($row_words=mysql_fetch_array($db_words)){
similar_text($row_words['name'],$word,$percent);
if(($percent>50)&&($percent!=100)){
echo '<strong>Did you mean:</strong><br>';
echo $row_words['name'],'<br>';
$flag=false;
}
}
}
?>

According to the comments below you are looking for something like this:
function corrected($bad, $good, $wholephrase) {
return preg_replace('#\b'. preg_quote($bad) . '\b#g', '<em>' . htmlspecialchars($good) . '</em>', $wholephrase);
}
For those of you who were interested in how the detection of misspelling and suggestion of alternatives is accomplished take a look here: Levenshtein Distance

No one helped me!! ;-(
Here is the code I created:
function suggestion($word){
$first_word=substr($word,0,1);
$first_word=strtoupper($first_word);
$query="SELECT * FROM words WHERE UPPER(LEFT(name,1))='$first_word'";
$db_words=mysql_query($query);
$wd=explode(" ",$word);
while($row_words=mysql_fetch_array($db_words)){
similar_text($row_words['name'],$word,$percent);
if(($percent>50)&&($percent!=100)){
$rw=explode(" ",$row_words['name']);
$d=array_udiff($rw,$wd,'strcasecmp');
for($i=0;$i<count($rw);$i++){
if(in_array($rw[$i],$d)){
$rw[$i]="<em>".$rw[$i]."</em>";
}
}
echo '<strong>Did you mean:</strong><br>';
foreach($rw as $r){
echo $r," ";
}
echo '<br>';
}
}
}

Related

How to create wordpress function?

i need to create function in word press function file to print some HTML cord. i tried this function.
function pre($chord){
echo '<pre>$chord</pre>'
}
and i use it in my post like this
<?php pre("sd");?>
and it will not work. please help me.
You are building your string incorrectly. when mixing html and php strings you need to concatenate the data like so...
function pre( $chord ) {
echo '<pre>' . $chord . '</pre>';
}
Also bear in mind that Wordpress is a large and modular CMS so by naming your function something as simple as pre you run the risk of conflicting with some other function called pre. It's good practise to prefix your functions with a unique string of letters like so...
function abc_pre( $chord ) {
echo '<pre>' . $chord . '</pre>';
}
Use whatever works for you.
Dan

Simple_html_dom - Find first line only

I use Simple_html_dom and I want it to show the first span.muted it finds on the page.
foreach($html->find('span.muted') as $e)
echo $e->innertext . "<br>";
Currently it execute a list of all the span.muted, and can't really seems to make it work like I want it.
This is what it looks like when i use the code.
3.1
Version: 3.1
Version: 3.1
Version: 3.1
And only want it to show 3.1 in this example.
If you want to show only the first one it finds without modify much of your code. Maybe you can use break like this.
foreach($html->find('span.muted') as $e) {
echo $e->innertext . "<br>";
break;
}
Or you can simplify it to:
echo $html->find('span.muted')->innertext . "<br>";
If the result is accurate and you're looking to grab only the version number regardless of the text that precedes it, you could use regular expressions on the result:
foreach($html->find('span.muted') as $e)
if(preg_match('/\d+\.\d+$/', $e->innertext, $matches)) {
echo $matches[0] . '<br />';
}
}
It looks like this:
echo $html->find('span.muted', 0)->innertext;

How to echo an function

I've been struggling to echo the output of a function. I tried this:
echo 'myFunction('foo')';
.. which obviously won't work, due to the extra single quotes. Any suggestions?
Let's take this function :
function getStr()
{
return "hello";
}
It will simply return a string, which means, calling this :
echo getStr();
Has the same exact result as calling this :
echo "hello";
Which means, the result of your function can be treated just like a variable (except you cant modify it), so you can do whatever you want with the result :
$string = getStr() . ' - ' . getStr();
echo $string; // Will print "hello - hello";
After trying a little while, I tried calling echo as an function:
echo (myFunction('foo'));
This works perfectly. I couldn't find this elsewhere on the internet (maybe I'm just a bad googler). Anyways, I thought I could might share this with you guys. In case anyone ever runs into the same problem.
Try this:
echo myFunction('foo');

Regex to get a specific function code from php file

I'm editing a PHP file to replace a function's code by another code provided.
My problem is that the function's code may change. So, I don't know exactly how I should do the regex to find it.
Here's an example:
function lol()
{
echo('omg');
$num=0;
while($num<100)
{
echo('ok');
}
}
function teste()
{
echo('ola');
while($num<100)
{
echo('ok');
}
}
function test()
{
echo('oi');
while($num<100)
{
echo('ok');
}
}
What I need is to get the code of function teste(), which would be:
echo('ola');
while($num<100)
{
echo('ok');
}
BUT: I do not know how many whiles, foreaches or how many brackets are inside the functions, neither I don't know it's order. The example was just an example.
How would I be able to get the function's code?
Thank you in advance.
Disclaimer
As other users stated, this is probably not a good approach. And if you decided to use it anyway, double-check your result as regex can be treacherous.
Ugly solution
You can you something like to match even if the function is the last one (#Nippey):
(?:function teste\(\))[^{]*{(.*?)}[^}]*?(?:function|\Z)
Note: I'm using (?:xyz) which is for non-capturing parentheses group. Check if your regex engine support it.
Output
echo('ola');
while($num<100)
{
echo('ok');
}
Using recursive pattern (?R) you can match nested brackets:
$result = preg_replace_callback('#(.*?)\{((?:[^{}]|(?R))*)\}|.*#si', function($m){
if(isset($m[1], $m[2]) && preg_match('#function\s+teste\b#i', $m[1])){
return $m[2];
}
}, $code); // Anonymous function is used here which means PHP 5.3 is required
echo $result;
Online demo

How can I make my function execute even when no parameter is entered?

I'm teaching myself PHP through a book and to make this easier I made myself a function to save myself having to write echo "<br/>"; a million times. My function works fine but after accidentally leaving out a parameter and getting an error I tried to make it so that if I left the function empty it would simply add one <br/> but I can't seem to get it to work.
Here is my code after my last attempt (which was to try case NULL: echo "<br/>";):
function br($break){
switch ($break){
case 1:
echo "<br/>";
break;
case 2:
echo "<br/><br/>";
break;
case 3:
echo "<br/><br/><br/>";
break;
case 4:
echo "<br/><br/><br/><br/>";
break;
case 5:
echo "<br/><br/><br/><br/><br/>";
break;
case NULL:
echo "<br/>";
}
}
Any ideas? Thanks guys!
P.s. Sorry if this is a really silly question, I only started learning PHP on Monday and my programming experience is somewhat limited :)
Try it like this:
function br($break = 1){
}
Btw, for your case, you can actually use:
echo str_repeat('<br />', 5);
Which will create 5 br's, take a look here: http://php.net/str_repeat
Also, when you are in doubt for something, first time check php.net documentation, there might be a function that does what you want already.
If yopu want 1 br by default, You can do this:
function br($break = 1){
}
It is probably easier to not use switch at all and instead do something like this perhaps:
function br($break = null)
{
if(!is_null($break)) {
echo str_repeat('<br />', (int) $break);
} else {
echo '<br />';
}
}
(not tested)

Categories