1

I have these arrays (array and array2)

$urls = array("http://piggington.com/pb_cash_flow_positive")

I have this regular expression

(preg_match("/\/{2}.*?\./", $array[$i], $matches))

It checks for everything that comes after 2nd slash and before 1st dot. So it will find

/piggington.

Now want to concatenate a variable inside the following regular expression, so it will search for a specific string.

I tried:

$matches_imploded = implode($matches);
$matches_imploded = preg_quote($matches_imploded, '/');
$match_with_other_array = preg_grep("/\/{2}".$matches_imploded."\./", $array2);

But it's not finding any matches.. What am I doing wrong? It should be looking inside array2 and making a positive match with $matches_imploded

between second slash and first dot we found $matches_imploded
1
  • note that your regex would match //piggington not /piggington Commented Jul 17, 2015 at 3:26

1 Answer 1

2

To match everything which comes after // and before the first dot, you need to use \K or positive lookbehind.

preg_match("~/{2}\K[^.]*(?=.)~", $array[$i], $matches)
$matches_imploded = implode($matches);
$matches_imploded = preg_quote($matches_imploded, '/');
$match_with_other_array = preg_grep("/\/{2}".$matches_imploded."\./", $array2);
Sign up to request clarification or add additional context in comments.

1 Comment

ok, thank you and "preg_grep("/\/{2}".$matches_imploded."\./", $array2);" would be the proper way to concatenate it?

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.