[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to extract data from a string in PHP?

Posted on 2005-04-11
2
Medium Priority
?
740 Views
Last Modified: 2008-01-09
Hello,

I have a string:

$str="Jim has 3 APPLES:";

OR

$str="Mrs Jones bought 6 ORANGES:";

I need to extract:
$name1 = "Jim" OR "Mrs Jones"
$name2 = "APPLES" or "ORANGES"
$number = "3" or "6"

name1 can be of 1 word or 2 words. name 2 is always 1 word in capital letters.

In PHP, how can I do the above?

Please help...

Thanks.

Jong

0
Comment
Question by:ccjong
2 Comments
 
LVL 6

Accepted Solution

by:
peyox earned 2000 total points
ID: 13759225
This is a good one :)

<?
   $str = "Jim has 3 APPLES";
   $str = "Mrs Jones bought 6 ORANGES";

   $words = explode (" ", $str);

   for ($i=0; $i<sizeof($words); $i++)
   {
      if (strtoupper($words[$i])==$words[$i]) $name2 = $words[$i];
      if (is_numeric ($words[$i])) $number = $words[$i];
   }

   $name1=$words[0];

   if ($words[0]=="Mr" || $words[0]=="Mrs" || $words[0]=="Miss" || $words[0]=="Ms")
      $name1 .= " ".$words[1];

   // display result

   echo "name1: ".$name1."<br>";
   echo "name2: ".$name2."<br>";
   echo "number: ".$number."<br>";
?>
0
 

Author Comment

by:ccjong
ID: 13759322
Thanks peyox.

I modified a little and it works perfactly.

Jong
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question