/    Sign up×
Community /Pin to ProfileBookmark

getting the length of a string in an array

I need to omit words that are less then 4 characters in my search query.

[code=php]<?php
$VarSearch_Criteria = ($_POST[“TxtSearch”]);
$outputsearch = explode(” “,($VarSearch_Criteria));
$matches = implode(“%’ OR Key_Words LIKE ‘%”,$outputsearch);
$charlength = strlen($outputsearch);
if ($VarSearch_Criteria == ”) {
echo “Please Enter Search Criteria”;
}
else
{
print_r($outputsearch);
echo($charlength);
$query = (“select * FROM Chamber_Members WHERE Key_Words LIKE ‘%” .$matches . “%’ AND $charlength > 3”);
$result=mysql_query($query) or die (‘Error: ‘.mysql_error ());

echo($query);

while($row = mysql_fetch_array($result))
{
$logo = trim($row[‘Business_Image’]);
$output .= “<h3>” . $row[‘Business_Name’] . “</h3>” . “<br />”;
$output .= $row[‘Business_Address’] . “<br />”;
$output .= $row[‘City’] . “, ” . $row[‘State’] . “<br />”;
$output .= $row[‘Zip’] . “<br />”;
$output .= “Phone: ” . $row[‘Business_Phone’] . “<br />”;
$output .= “Fax: ” . $row[‘Business_Fax’] . “<br />”;
$matched_keywords == “”
?>[/code]

for some reason this always returns a value of 5 for $charlength I am sure the issue is that I am looking at an array.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@gavshouseMay 14.2010 — You could try

[code=php]foreach($outputsearch as $word){
if(strlen($word) <= 3){
//less then 4
}
}[/code]
Copy linkTweet thisAlerts:
@hotrodjeremyauthorMay 14.2010 — Not sure where to put that casue it still puts "and" and "the" in the query

select * FROM Chamber_Members WHERE Key_Words LIKE '%computers%' OR Key_Words LIKE '%and%' OR Key_Words LIKE '%electronics%'

would like the php code to omit words like "and", "the" ect.
Copy linkTweet thisAlerts:
@svidgenMay 14.2010 — You may have to use something more similar to the solution I initially recommended for this issue:
[code=php]// the regex is updated to resolve the mistake in my original suggestion
$wordlist = preg_split("/[, ]+/", $_POST['TxtSearch']);
foreach ($wordlist as &$word) {
// the conditional here is new, omitting words under 4 characters
if (strlen($word) > 3) {
$word = "Key_Words like '%" . mysql_escape_string($word) . "%'";
}
}

if (sizeof($wordlist) > 0) {
$where_clause = 'where ' . join(' or ', $wordlist);
} else {
$where_clause = '';
}

$query = "select * from Chamber_Members {$where_clause}"; [/code]
×

Success!

Help @hotrodjeremy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...