-1

In $contents, I'd like to grab everything after the pipe e.g. boss.

$search_for = "micky.mcgurk";
$contents = "micky.mcgurk@somewebsite.com|boss";
$pattern = sprintf('/\b%s@([^|\s]+)\|/m', preg_quote($search_for));

if (preg_match_all($pattern, $contents, $matches)) {
    $submitted_by = implode("\n", $matches[1]);
} else {
   echo "No user found";
}
michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190

4 Answers4

4
$resultat = substr(strrchr($content, '|'), 1);
CyrielleDev
  • 156
  • 6
1

Why not just use explode?

$contents = "micky.mcgurk@somewebsite.com|boss";
$result = explode("|", $contents);
echo $result[1];

This requires, that mail address and the content after "|" is not having a "|" also ... ;)

SiL3NC3
  • 690
  • 6
  • 29
1

RegExp way:

$pipe = preg_quote('|', '/');
$regExp = '/.*' . $pipe . '(.*)$/';

The fastest way:

$name = substr($string, strpos($string, '|') + 1);
Sergej
  • 2,030
  • 1
  • 18
  • 28
1

To answer about what's happening with your current regex:

([^|\s]+)

matches every continuous non | and whitespace character. Once it encounters that it stops. Because of the capture groups () that is captured.

https://regex101.com/r/DVY3F0/2/

What you want is directly after that though. If you add:

\|\K.*

and remove that previous capture group you will have you want at the 0 index.

https://regex101.com/r/DVY3F0/3/

In full form:

\bmicky\.mcgurk@[^|\s]+\|\K.*

or you could just have it as a vague check for non | until a |.

[^|]+?\|\K.*

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

user3783243
  • 5,368
  • 5
  • 22
  • 41