Solved

PHP Array Output Formatting

Posted on 2008-06-23
7
357 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
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…

821 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