Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matching multiple strings in a string

Status
Not open for further replies.

DeepBlerg

Technical User
Jan 13, 2001
224
AU
Hi,

If I have a string like this:

Lots of text here. Lots of text here. Name: Someone ID: 93493284 Lots of text here. Type: person Lots of text here. Lots of text here.

and I need 3 vars created, $name, $id and $type from the string, whats the best way of doing this?

Thanks.
 
Assuming that the ID immediately follows the name, and that neither an ID nor a type has any spaces in it, then this is one way:

Code:
<?php
$text = 'Lots of text here. Lots of text here. Name: Someone ID: 93493284 Lots of text here. Type: person Lots of text here. Lots of text here.';

$data = preg_replace ('/^.*Name: (.*) ID: ([^ ]+).*Type: ([^ ]+).*$/', &quot;$1\t$2\t$3&quot;, $text);

list ($name, $id, $type) = split (&quot;\t&quot;, $data);
?>
Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top