-1

I want to grab the full contents of the value utm_campaign from the $page variable using the following PHP:

$page = "utm_source=google&utm_campaign=Test | Test - Exact&utm_test=test";
preg_match_all('#utm_campaign=([^\s&]+)#', $page, $matches);
var_dump($matches);

However the above var_dump only outputs the following:

Test

michaelmcgurk
  • 6,137
  • 22
  • 81
  • 184
  • 2
    Well, your regex contains `\s` which means it's stopping as soon as it finds a space. So if you remove `\s` your regex would work. However, it's very strange for an URL to contain spaces, they should be encoded as %20, so I think you should revisit how you obtained `$page` in the first place – Andrea Olivato Jun 29 '21 at 14:52
  • I've run this on three different versions of PHP and the result is identical each time: `array(2) { [0]=> array(1) { [0]=> string(15) "utm_campaign=Te" } [1]=> array(1) { [0]=> string(2) "Te" } }`. Are you _certain_ that it is outputting just `Test` and not more? – Martin Jun 29 '21 at 14:53
  • Maybe parse_str is what you're looking for: https://www.php.net/manual/en/function.parse-str.php – Martijn Jun 29 '21 at 14:58

2 Answers2

4

Try parse_str instead:

$page = "utm_source=google&utm_campaign=Test | Test - Exact&utm_test=test";
$res = [];
parse_str($page, $res);
var_dump($res);

This gives you:

array(3) { ["utm_source"]=> string(6) "google" ["utm_campaign"]=> string(19) "Test | Test - Exact" ["utm_test"]=> string(4) "test" }
nitrin0
  • 572
  • 2
  • 9
0

Your regex is excluding spaces and &, this is what ^ does int [^\s&] so the match stops after the space in test. If you want the whole thing, change your regex to #utm_campaign=([^&]+)#

Using an online regex tester you can see what is being matched and not.

https://regex101.com/r/Frxwg0/1/

Schleis
  • 37,924
  • 7
  • 65
  • 84