Ryan Litwiller
Ryan Litwiller

Reputation: 507

Regular Expression Help - PHP - preg_match()

Hope I don't get beaten up about this. I'm new to regex and I know there are tons of resources out there, but my problem is that I want to do a few specific things and I cant seem to figure out how to piece the expression together.

Say this is a snippet of my string:

AudienceTypes=Internal; InternallearnerName=Ryan Litwiller; uName=227812;

I want to search for InternallearnerName= and store what comes after = up to the semi colon as a variable.

As an alternative to preg_match() I have tried to start working out a solution using preg_split by semicolon, array_search(), then substr() but really thought a regex would be a much simpler solution.

Upvotes: 1

Views: 137

Answers (4)

cagri
cagri

Reputation: 828

Some examples

Using preg_match():

$text = "AudienceTypes=Internal; InternallearnerName=Ryan Litwiller; uName=227812;";
preg_match("/InternallearnerName=(.*?);/i",$text,$output);
echo $output[1];

Output:

Ryan Litwiller

You can use explode() alternatively. It's long way.

$text = "AudienceTypes=Internal; InternallearnerName=Ryan Litwiller; uName=227812;";
$value = explode("InternallearnerName=",$text);
$value = explode(";",$value[1]);
echo $value[0];

Output:

Ryan Litwiller

Upvotes: 1

Kenney
Kenney

Reputation: 9093

Here you go:

<?php
$s='AudienceTypes=Internal; InternallearnerName=Ryan Litwiller; uName=227812;';
preg_match( '/\bInternallearnerName=([^;]+)/', $s, $matches );
echo $matches[1], "\n";

The regular expression:

  • \b means 'word boundary', so it won't match FOOInternallearnerName=...
  • (..) defines a group; the first pair will be number 1.
  • [^..] is a single character that is NOT (^) one of the following; in this case ;. So, it matches any character except ;.
  • + means 1 or more.

$matches will store the matches. $matches[0] will store the entire matched string, and $matches[1] the first group. ($matches[2] would store the second, except you only have one group (..) in the regular expression.

To match the value of uName aswell use this:

preg_match( '/\bInternallearnerName=([^;]+.*?\buName=([^;]+)/', $s, $matches );

Here, $matches[2] will be 227812.

Note that the first and last character of the regular expression, / in this case, are only there to mark the beginning/ending of the regular expression. Strictly speaking they are not needed, but this is probably legacy. You can use other characters there aswell; for instance, if you're matching URLs or file paths you could use @: preg_match( "@.....@", ....).

Also, preg_match returns false if there is no match, so you should check for it:

Upvotes: 3

John Vinopal
John Vinopal

Reputation: 561

Regex:

\bInternallearnerName=([^;]+)

What this means:

  • find a word break (non-alphanum character)
  • find InternallearnerName= anywhere in the string
  • start a tagged match (eg: capture text) (
  • find one-or-more + characters from the set [ ] that does not ^ contain a semi-colon ;
  • end the tagged match )

Note that this fails for:

InternallearnerName=;

If you care about that case, change the + (one-or-more) to a * (zero-or-more)

Upvotes: 1

Phantom
Phantom

Reputation: 388

Work in steps:

  1. Use explode(",", $string);: this creates an array. Its entries should look like: {[0]=> "AudienceTypes=Internal", [1]=>" InternallearnerName=Ryan Litwiller"} .... Notice the spaces. Try to clean them out.

  2. Go on every array item, and split it again, just like before. Then, make use of the explode() function again, this time explode("=", $array[$arrayItemNum]);.

See the PHP Manual explode() page for more detailed info.

Upvotes: 0

Related Questions