Solved

PHP Array Output Formatting

Posted on 2008-06-23
7
371 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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.

729 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