[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PHP Array Output Formatting

Posted on 2008-06-23
7
Medium Priority
?
385 Views
Last Modified: 2013-12-13
Hi,

I am trying to format my array output as below:
I have the following output in the browser when I run my php script.
O/P :
Array ( [0] =>
the site abc.com has been visited for 100 times in june
)

Array ( [0] =>
the site cdf.com has been visited for 200 times in june
)
......
...........list goes on...

I would like to just take the site name and the number of times i.e "abc.com" & "100" and store in the database.
Please let me know how do I achieve this and also if possible how to store these two fields alone (for all records)  into the database.

please let me know if I need to be more clear.
thanks.
0
Comment
Question by:Igiwwa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 21847095
Well, you would need a table to hold the results

CREATE TABLE myResults (

    siteName VARCHAR(128),
    siteCount INT DEFAULT 0
);

Assuming the text "the site cdf.com has been visited for 200 times in june" is regular and always has that format then you can explode the spaces out and simply pick out the 3rd and 8th entries


// For convenience I'll just set this to be a string - you would
// get it from your array
//
$arrayText = "the site cdf.com has been visited for 200 times in june";

// Split it up on the spaces
//
$txt = explode( " ", $arrayText );

// Put it in the database table
//
@mysql_query("insert into myResults (siteName, siteCount) VALUES( '".$txt[2]."', ".intval($txt[7])." ) ");

0
 

Author Comment

by:Igiwwa
ID: 21855177
Hi,
thanks for the reply. This works fine.thanks again.One more question here.
Is there any way split the following data.
I get a list like this as O/P
Array ( [0] => 1. one
2. two
3. three
4. twenty two
5.five
6. six
7. seventy eight

)


I want to format this output as below

one
two
three
twenty two
five
six
seventy eight

and same way have to store in the database too.

thanks.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 21855241
I'm not sure I understand the thrust of your question. can you be more concise?
0
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.

 

Author Comment

by:Igiwwa
ID: 21855312
sure. I have a different script which gives the o/p like this.
(script is querying  a site and the o/p is as below)

Array ( [0] => 1. one
2. two
3. three
4. twenty two
5.five
6. six
7. seventy eight

)

I want to store this o/p in the database but I want to store just the words not with the numbers.So I would like to format the output(using regex??) as below so that I can just store these words into the database.


one
two
three
twenty two
five
six
seventy eight


Is this clear??if not please let me know again.

Thanks.
0
 

Author Comment

by:Igiwwa
ID: 21855511
sure. I have a different script which gives the o/p like this.
(script is querying  a site and the o/p is as below)

Array ( [0] => 1. one
2. two
3. three
4. twenty two
5.five
6. six
7. seventy eight

)

I want to store this o/p in the database but I want to store just the words not with the numbers.So I would like to format the output(using regex??) as below so that I can just store these words into the database.


one
two
three
twenty two
five
six
seventy eight


Is this clear??if not please let me know again.

Thanks.
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 21858786
Try this


$aString =  "1. two  2. seventy eight 3. twenty";
$results = array();

preg_match_all("/([0-9])(\.\s)([a-zA-Z| ]{1,})/", $aString, $results );

foreach ( $results [3] as $anAnswer )
     echo "$anAnswer<br>";
   
0
 

Author Comment

by:Igiwwa
ID: 21860588
Hey

thanks! it worked . I am just adding the mysql query line as my question is with storing into sql. might be useful if anyone needs it. thanks again.

$aString =  "1. two  2. seventy eight 3. twenty";
$results = array();

preg_match_all("/([0-9])(\.\s)([a-zA-Z| ]{1,})/", $aString, $results );

foreach ( $results [3] as $anAnswer ) {
        echo "$anAnswer<br>";
      @mysql_query("INSERT  INTO words_table (words) VALUES( '".$anAnswer."') ");
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

650 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