Pull word from sentence
is there a way to find a word in a sentence using PHP? We have a form and in the subject line we want to redirect someone if they use the words delivery, deliver, or delivered. Can this be done and how would it be? thank you for any help on this subject.开发者_运维知识库
This should do it:
if ( preg_match('/deliver(?:y|ed)?/i', $subject) )
{
// Redirect
}
You can use the strpos
function to search for a string inside another string. If the string is not found false
will be returned, and you know that your string was not found.
another method:
if (stristr($sentence,"deliver")) {
header('location: somepage.php');
}
But I would use preg_match as expressed before.
One method of many:
if (preg_match('/deliver(y|ed)?/', $string)) {
// yes, $string contained 'deliver', 'delivery' or 'delivered'
}
Here:
<?php
if (strpos($string, "deliver")) {
header("Location: somepage.php");
}
?>
- to extract all words, simple explode the string using a whitespace characters
to check a particular word either use the simple strpos() function or a regular expression pattern matching
preg_match("/(?:^|\s+)deliver(y|ed)?(?:$|\s+)/i")
The above expression checks whitespace character or beginning of string, similary whitespace character or end of string
精彩评论