/    Sign up×
Community /Pin to ProfileBookmark

Replace comma for OR

I have this script on my site that suggests properly spelled words if a person searches by a misspelled word.

The spelling suggestions are contained in a variable which is defined by the PHP script segment at the bottom of this post.

Right now the output looks like this : “flood, flowood”, but with the rest of the script it reads like this: “Found 0 definitions found for floood. May we suggest: flood, flowood”.

Is there a way, and if yes, what would I do to replace the last comma in my string with “or”, so I would get “flood or flowood”?

Thanks

[code=php] // here we build the suggestions output

$terms=array();
$out=”;
foreach($res as $entry) {
if($entry) {
$match = explode(” “,rtrim($entry));
$terms[] = strtolower(str_replace(“””, “”, $match[1]));
}
}

function phoebe_suggestionLink($term) {
return “<a href=”$_SERVER[$PHP_SELF]?w=”.urlencode($term)
. ‘”>’.htmlentities($term).'</a>’;
}
if($terms) {
sort($terms);
$terms=array_map(‘phoebe_suggestionLink’,array_unique($terms));
$out=join(‘, ‘,$terms);
}
}
}
return $out;
}[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@pyroJan 31.2005 — [code=php]str_replace(',', ' or', $string);[/code]
Copy linkTweet thisAlerts:
@NogDogJan 31.2005 — This would change only the last comma in the series to "or":
[code=php]
<?php
$text = "one, two, three, four";
$text = preg_replace("/,([^,]*)$/", " or $1", $text);
echo $text;
?>
[/code]
Copy linkTweet thisAlerts:
@pyroJan 31.2005 — Oops... didn't read the post well enough.
×

Success!

Help @shopkanji 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 4.27,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...