Jump to content

parsing a string of text


supermerc

Recommended Posts

Hey

 

I have a variable $v[1] that either turns out as:

 

Deposited item (#######)

 

or:

 

Awarded item (#######) to Name

 

I want to separate those into different variables. For example if its Deposited I want item to be in separate variable alone and the ######## to be in another variable alone.

 

If its awarded I want Item and ###### to be in variables alone but I also want Name to be in a variable alone as well.

 

Can anyone help me?

 

 

Link to comment
https://forums.phpfreaks.com/topic/140033-parsing-a-string-of-text/
Share on other sites

Really? Because when I do this:

<?php
$string[] = "Deposited item (#######)";
$string[] = "Awarded item (#######) to Name";

foreach ($string as $s) {
   preg_match("~(?:Deposited|Awarded)\s([^\s]+)\s\(([^\)]+)\)(?:\sto\s([^\b]+))?~",$s,$match);
 echo "<pre>"; print_r($match); echo "</pre>";
}	 
?>

 

I get

 

Array
(
    [0] => Deposited item (#######)
    [1] => item
    [2] => #######
)

Array
(
    [0] => Awarded item (#######) to Name
    [1] => item
    [2] => #######
    [3] => Name
)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.