Solved

PHP Array Output Formatting

Posted on 2008-06-23
7
354 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now