Solved

PHP Array Output Formatting

Posted on 2008-06-23
7
353 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 29
php connect() failed error 25 17
Tracking Bouncebacks in PHPMailer 3 10
inserting while loop results 2 7
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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…

760 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

22 Experts available now in Live!

Get 1:1 Help Now