Jump to content

[SOLVED] Split a string into words, then add a * onto each word


bluedaniel

Recommended Posts

So im doing a FULL TEXT Boolean search with multiple values from one string which should be split using a comma although perhaps spaces should also be included for simplicity.

 

Also each word should have a + symbol before each word.

 

$filter_array = array(", ");
$sqlinclude = mysql_real_escape_string(str_replace($filter_array," +",$include));

 

Right now its basic, so "Chicken, tomato" becomes "+Chicken +tomato" as there is a + symbol at the start of the statement in SQL.

 

The star at the end would mean that "tomato" also becomes "tomatoes" which is very important.

 

Thanks guys

Link to comment
Share on other sites

The php GET code:

$include = "0";
if (isset($_GET['include'])) {
  $include = $_GET['include'];

	$filter_array = array(", ", "\r", "\n");
	$sqlinclude = mysql_real_escape_string(str_replace($filter_array," +",$include));
}

 

The SQL code:

$query_finder = "SELECT Title, SmallDesc, id, picturesmall FROM recipes WHERE MATCH (Ingredients) AGAINST ('+$sqlinclude -$sqlexclude' IN BOOLEAN MODE) AND type='$type'";

 

I need to split $include into words, add a + before and a * after so "Chicken, tomato" becomes "+Chicken* +tomatoes*"

 

now i get +Chicken +tomato

Link to comment
Share on other sites

Two methods:

 

<?php
// extracting into an array to add the *
$filter_array = array(", ", "\r", "\n");
$sqlinclude = mysql_real_escape_string(str_replace($filter_array," +",$include));
$words = explode(" ",$include);
$output = "";
foreach($words as $word){
  $output .= $word."*";
}
$sqlinclude = implode(" ",$output);
?>

 

OR this might work

 

<?php
// simply adding the * in front of the replacement string
$sqlinclude = mysql_real_escape_string(str_replace($filter_array,"* +",$include));
?>

Link to comment
Share on other sites

I was hoping for a foreach loop as I feel it might be more robust (is it?) but the first method doesnt work,

 

Ive already tried the second method although it doesnt add a star to the last word in the string as it doesnt have a comma after it.

 

I could just add a star to the end of the string but is there a simpler way for the foreach loop?

Link to comment
Share on other sites

Working example based on your second method:

 

$include = "0";
if (isset($_GET['include'])) {
  $include = $_GET['include'];

	$filter_array = array(", ", "\r", "\n");
	$hello .= $include."*";
	$sqlinclude = mysql_real_escape_string(str_replace($filter_array,"* +",$hello));
}

 

The output of the first logic:

 

SELECT Title, SmallDesc, id, picturesmall FROM recipes WHERE MATCH (Ingredients) AGAINST ('+ -' IN BOOLEAN MODE) AND type='0'

 

It breaks completley

Link to comment
Share on other sites

lol I made a really obvious mistake in my first method.  If you're still interested could you give this a try?  This should be at least a little closer :)

 

<?php
// extracting into an array to add the *
$filter_array = array(", ", "\r", "\n");
$sqlinclude = mysql_real_escape_string(str_replace($filter_array," +",$include));
$words = explode(" ",$include);
$output = array();
foreach($words as $word){
  $output [] = $word."*";
}
$sqlinclude = implode(" ",$output);
?>

Link to comment
Share on other sites

lol your right that is a lot closer! its missing the + symbol at the beginning of each word but I think I can fix that.

 

Are you good with FULLTEXT? im really learning quick and getting better results each time, I wanted to return my results with a COUNT of line breaks in the Ingedients field with the lowest count coming first, any ideas what direction I should take?

Link to comment
Share on other sites

yeah thats what I thought, to make it even more complicated I wanted a mix of Sort by Relevance and Ingredient count combined.

 

Got the relevance sorted with

 

"SELECT *,MATCH(Ingredients) AGAINST ('+$sqlinclude -$sqlexclude' IN BOOLEAN MODE) AS relevance FROM `recipes` WHERE MATCH(Ingredients) AGAINST ('+$sqlinclude -$sqlexclude' IN BOOLEAN MODE) ORDER BY relevance DESC"

 

If anyone has any clues as how to go about this feel free to post!

 

Thanks for your help p2grace anyway! my website is http://www.whatcouldicook.com/ and the code is used on the recipe finder on the first page, give it a try if you like cooking!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.