0

I just got some really strange problem.

Check it out:

$yourWord='http://mywebsite.com/combo-points-carmaboy-boys-sixer';
$URLW = "'kid', 'kids', 'boys', 'six'";
$targets = array($URLW);


$pattern = '/\b('.implode('|', $targets).')\b/';

if (preg_match($pattern, $yourWord, $matches)) {
echo 'FOUND!!';
} else {
echo 'NOT FOUND!';
}

In this way it's giving me result NOT FOUND! but when i do that $targets = array('kid', 'kids', 'boys', 'six'); it's working.

So why it's not working on the way i make it in my code sample ?

And how i can use it with variable in the array ?

Thanks

2
  • 1
    $URLW is a string, it wont get evaluated into an array structure because it looks like one. Commented Jul 12, 2014 at 20:26
  • PHP users are so used to evaluate strings to whatever type or construction they want, that they forget how the programming really works... Commented Jul 12, 2014 at 20:44

4 Answers 4

2

$targets = array("'kid', 'kids', 'boys', 'six'");

is a an array consisting of a single element that's a comma separated string

$targets = array('kid', 'kids', 'boys', 'six');

is an array consisting of 4 string elements

Sign up to request clarification or add additional context in comments.

Comments

1

To explain why it's not found, array($URLW) returns an array with ONE element, as it's ONE string containing commas. This makes implode() return false (as it expects the first argument to be an array, and not a string), and your statement completely different than you expected (instead it should convert the value int(0) into a string in the regex-pattern within the word-boundaries, which isn't what you want, and which doesn't exist in your pattern). If you need to use the string with commas, split it first using explode(), then keep that array:

$URLW = explode(", ", "'kid', 'kids', 'boys', 'six'");

If you don't need to use it, then simply declare the array with the regular syntax:

$URLW = Array("kid", "kids", "boys", "six");

Comments

0

Hi whats this all about?

$URLW = "'kid', 'kids', 'boys', 'six'";
$targets = array($URLW);

Try like this

 $targets = array('kid', 'kids', 'boys', 'six');

Here is your regx before changing it, by the way.

 /\b('kid', 'kids', 'boys', 'six')\b

Here is the regx you wanted,

 /\b(kid|kids|boys|six)\b

And if you need this bit $URLW as a string

 $URLW = "'".implode("', '", $targets )."'";

Comments

0

$URLW is a string, array() won't be able to convert the comma separated items to expected array format. You either need to initialize the array properly:

$targets = ('kid', 'kids', 'boys', 'six');

Or convert the string to an array using something like this:

$targets = array_map(function($target) {
  return trim($target, "' ");
}, explode(',', $URLW));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.