Jump to content

Recommended Posts

OK. I have a wordlist and a few words contain a hyphen (-).

 

I'm currently using an array_search to search a string with the words from the wordlist, but I think a regular expression would be better.

 

How can I search the string with words from the wordlist the easiest and fastest with preg_match

Link to comment
https://forums.phpfreaks.com/topic/265178-searching-a-word-with-a-hyphen/
Share on other sites

Post the code you're using, and some of the data that fails when using it.

 

$data = "Hyphen-"; // Would usually be $_POST data
// Hyphen- returns "Worked". Hyphen-Test returns "Didn't".
$blocked = array("Works", "Hyphen", "Hyphen-");
if (array_search($data, $blocked))
{
echo "Worked";
}
else echo "Didn't";

Both of these work properly:

$data = "Hyphen-Test";
$blocked = array("Works", "Hyphen", "Hyphen-Test");
if ( array_search($data, $blocked) !== FALSE ) {
echo "Worked";
} else {
echo "Didn't";
}

// *** AND ***
$data = "Hyphen-";
$blocked = array("Works", "Hyphen", "Hyphen-");
if ( array_search($data, $blocked) !== FALSE ) {
echo "Worked";
} else {
echo "Didn't";
}

 

The change to a comparison using !== FALSE is because array_search returns the array key (which could be zero) if the value is found, or FALSE if the value is not found.

 

EDIT: That change ^^^ would not affect the outcome using the data you provided, however.

Both of these work properly:

$data = "Hyphen-Test";
$blocked = array("Works", "Hyphen", "Hyphen-Test");
if ( array_search($data, $blocked) !== FALSE ) {
echo "Worked";
} else {
echo "Didn't";
}

// *** AND ***
$data = "Hyphen-";
$blocked = array("Works", "Hyphen", "Hyphen-");
if ( array_search($data, $blocked) !== FALSE ) {
echo "Worked";
} else {
echo "Didn't";
}

 

The change to a comparison using !== FALSE is because array_search returns the array key (which could be zero) if the value is found, or FALSE if the value is not found.

 

EDIT: That change ^^^ would not affect the outcome using the data you provided, however.

 

But if I add something after the "Hyphen-" it returns the opposite.  :confused:

 

EDIT: I don't think I made the post clear. I need it to search the whole word for that particular phrase. Not matching the blocked word with one word on it's own if you know what I mean

The first block I posted has something after the hyphen, and it works as written.

But then if I remove the "Test" it returns the opposite. The thing is, it will be $_POST data so it could be anything. I want it to find, let's say: Hyphen- from Hyphen-Test and return it true/false. I want it to search the whole word for a blocked word and if it has it anywhere in the word return true/false.

That isn't the way you explained it to begin with. "hyphen-test" is a hyphenated word, "hyphen-" is a fragment of that word. I think this is more what you're after.

 

$data = "Hyphen-test";

$blocked = array("Works", "Hyphen", "Hyphen-");

foreach( $blocked as $v ) {

if( stripos( $data, $v) !== FALSE ) {

echo "Found!";

break;

}

}

That isn't the way you explained it to begin with. "hyphen-test" is a hyphenated word, "hyphen-" is a fragment of that word. I think this is more what you're after.

 

$data = "Hyphen-test";

$blocked = array("Works", "Hyphen", "Hyphen-");

foreach( $blocked as $v ) {

if( stripos( $data, $v) !== FALSE ) {

echo "Found!";

break;

}

}

 

Yeah, that is what I wanted. I tried to explain it like that in another previous post (I think). Sorry about the confusion and thanks for the help.

That isn't the way you explained it to begin with. "hyphen-test" is a hyphenated word, "hyphen-" is a fragment of that word. I think this is more what you're after.

 

$data = "Hyphen-test";
$blocked = array("Works", "Hyphen", "Hyphen-");
foreach( $blocked as $v ) {
if( stripos( $data, $v) !== FALSE ) {
	echo "Found!";
	break;
}
}

 

That is what I wanted.. but I can't seem to get it to work in a function.

 

This is my current code(It returns as "no" whatever I use as $input):

function test($input)
{
$blocked = array("Works", "Hyphen", "Hyphen-");
foreach( $blocked as $v ) {
if( stripos( $input, $v) !== FALSE ) {
	echo "Found!";
	break;
 }
 else
	{
	echo "no";
	break;
	}
}
}

test("test");

Usually better to just return a value instead of echoing directly from within the function.

 

<?php
function test($input) {
$blocked = array("Works", "Hyphen", "Hyphen-");
foreach( $blocked as $v ) {
	if( stripos( $input, $v) !== FALSE ) {
		return 'Found';
	}
}
return 'Not found';
}
echo test("Hyphen"); // echoes 'Found'
echo '<br>';
echo test("not in the array"); // echoes 'Not found'

 

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.