S17514
S17514

Reputation: 285

How to read text after a specific character with PHP

Lately what I've wanted to do is read specific text after a specific character. What I want to do is read the numbers after an equal sign. This is the text that I have:

Lime Green = 13,
Sensei Gray = 14,
Aqua = 15,
Arctic White = 16,
Black Sunglasses = 101,
Dark Vision Goggles = 102,

The numbers that are after the "=" sign is the ID for the text. What I want to do is grab the ID for the text an place it in the variable $id, any help would be highly appreciated!

Upvotes: 0

Views: 732

Answers (3)

Altaf Hussain
Altaf Hussain

Reputation: 1048

Use regular expression for this

$content="Lime Green = 13,
Sensei Gray = 14,
Aqua = 15,
Arctic White = 16,
Black Sunglasses = 101,
Dark Vision Goggles = 102,";

preg_match_all("/=(.*),/siU",$content,$output);

print_r($output[1]);

Upvotes: 1

Phillip Schmidt
Phillip Schmidt

Reputation: 8818

  1. Set up an array with the different values/names.
  2. Loop through the array.
  3. Use explode("=") to split each string around the equals sign.
  4. Get the second half of each array (explode returns an array)

Upvotes: 0

jason
jason

Reputation: 1255

Use explode:

$parts = explode(' = ', rtrim($line, ','));

Then $parts[0] will be the phrase and $parts[1] will be the ID.

Upvotes: 2

Related Questions