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
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
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.