Gossamer Forum
Home : General : Internet Technologies :

Breaking Up a Line using PHP

Quote Reply
Breaking Up a Line using PHP
Is there anyway to break up a line like the following using PHP coding?

Gen:1:1: In the beginning God created the heaven and the earth.

I want to break this line up into Book, Chapter, Verse, and actual Verse. This line has colons for this specific reason, however using the split() function is only splitting up the book names and deleting what is left over. Here is the code I've been using, I know it's not what I want to do but will illustrate the example I'm speaking of.

Code:
<?$text = file("OT.dat");
for ($index=0; $index < count($text); $index++)
{

$textlist = split(":", chop($text[$index]));
echo ($textlist[0] . "<br />");
}
?>


Any help what so ever is much appreciated.
Quote Reply
Re: [JoFrRi] Breaking Up a Line using PHP In reply to
$details = explode(':', $line);
Quote Reply
Re: [Mark Badolato] Breaking Up a Line using PHP In reply to
May want to also use this bit too... to get rid of the leading spaces...

$details[3] = preg_replace("/^\s/", "", $details[3]);

Not sure if the formatting is correct, as I havn't used preg_replace() for a while Frown

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Breaking Up a Line using PHP In reply to
it be faster to use trim() I think
Quote Reply
Re: [Mark Badolato] Breaking Up a Line using PHP In reply to
Yeah, your right. I always forget about that function Frown

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Breaking Up a Line using PHP In reply to
Could you guys possibly tell me why I sometimes get an output that looks like the following?

Code:
ArrayArrayArrayArrayArrayetc.
Quote Reply
Re: [Andy] Breaking Up a Line using PHP In reply to
Thanks for your help guys. I finally figured out that which I needed to know. What I wasn't understanding is the way an array is setup and how you take data from it. If you guys are curious, this is what I've got to display what I wanted:

Code:
<html>
<body>
<table width="100%" border="0">
<?
$ot_load = file("OT.dat");




for ($index=0; $index < count($ot_load); $index++) {
$text = explode(":", chop($ot_load[$index]));

echo("<tr><td>" . $text[0] . "<br /></td>");
echo("<td>" . $text[1] . "<br /></td>");
echo("<td>" . $text[2] . "<br /></td>");
echo("<td>" . $text[3] . "<br /></td></tr>");
}

?>



</table>
</html>




Now what I need to do is write a script to add these to my database as they're read.
Quote Reply
Re: [JoFrRi] Breaking Up a Line using PHP In reply to
Because $details Is an array. You need to reference it with something like;


$details[0]
or
$details[1]

... or show the whole lot, something like;

Code:
foreach ($details as $var) { echo "$var<BR>"; }


Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Breaking Up a Line using PHP In reply to
>> ... or show the whole lot, something like;

a great way to dump out a whole structure in PHP, while keeping the structure visually intact:

Code:
echo '<pre>';print_r($details); echo '</pre>';
Quote Reply
Re: [Mark Badolato] Breaking Up a Line using PHP In reply to
Interesting. That may help in some of my debugging stuff for my PHP scripts. Thanks for sharing that Mark Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Breaking Up a Line using PHP In reply to
np :) yeah that one comes in useful. Of course, i really hate debugging in PHP and wish a decent IDE existed (Komodo was ok last time i looked, which was a long time ago, but nothing has blown me away yet)
Quote Reply
Re: [Mark Badolato] Breaking Up a Line using PHP In reply to
PHPEd is pretty good. I use that now. Great for finding missing { and }, and all that kinda stuff. Also, has a built in local testing platform (with PHP/Perl etc), which helps to debug the variables being passed into the PHP script, and outputs etc.

I'm pretty happy with it Smile

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Breaking Up a Line using PHP In reply to
I've tried PHPEd. Hated it. :)

I use Edit Plus as an editor which does practically everything I need as far as an editor, just doesn't have IDE functions like watch points or step thru's, etc
Quote Reply
Re: [Mark Badolato] Breaking Up a Line using PHP In reply to
Each to their own Sly

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Mark Badolato] Breaking Up a Line using PHP In reply to
Another nice technique for debugging and keeping your code clean:

error_reporting(E_ALL);

It's basically the equivalent of perl's "use strict;". I wouldn't use it on a live script, but it's great for development.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] Breaking Up a Line using PHP In reply to
I only wish I knew what you guys were talking about :P

However, another new problem has risen. While I was adding strings to the database with a script I've written, some lines (I'm guessing) that contain ' or " are not being added because of syntax errors. I have tried "addslashes()" to it however when I do that I'm losing all the values from the array. Here's what I've got:

Code:
<html>
<body> <table width="100%" border="1">
<?

// Connect to the database server
$dbcnx = @mysql_connect("localhost", "root");
if (!$dbcnx) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}

// Select the presenttruth database
if (! @mysql_select_db("presenttruth") ) {
echo( "<p>Unable to locate the presenttruth" . "database at this time.</p>" );
exit();
}



$ot_load = file("OT01.txt");

$ot_text = addslashes($_POST['ot_load']);

for ($index=0; $index < count($ot_text); $index++) {
$text = explode(":", chop($ot_text[$index]));

$addsql = "INSERT INTO Bible SET
bible_book='$text[0]',
bible_chap='$text[1]',
bible_verse='$text[2]',
bible_text='$text[3]'";

echo("<tr><td>" . $text[0] . "<br /></td>");
echo("<td>" . $text[1] . "<br /></td>");
echo("<td>" . $text[2] . "<br /></td>");
echo("<td>" . $text[3] . "<br /></td></tr>");




if (@mysql_query($addsql)) {
echo("<h5>Info added successfully.</h5>");
} else {
echo("<h5>Error adding info: " . mysql_error() . "</h5>");
}
}

?>

</table>
</html>



The $text output is just for debugging purposes so you can ignore that.

Last edited by:

JoFrRi: Aug 26, 2003, 3:06 PM
Quote Reply
Re: [JoFrRi] Breaking Up a Line using PHP In reply to
It looks like your SQL syntax for the insert query is wrong. I think it should probably be:

Code:
"INSERT INTO Bible (bible_book, bible_chap, bible_verse, bible_text) VALUES ('$text[0]', '$text[1], '$text[2]', '$text[3]')"

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] Breaking Up a Line using PHP In reply to
Nope, I got it to work. What I did was addslashes to the only part of the array that contained the ' values.

Code:
$text[3] = addslashes($text[3]);


Instead of adding slashes to the entire array.

Thanks for your help though. Smile
Quote Reply
Re: [JoFrRi] Breaking Up a Line using PHP In reply to
Yep - my mistake. I didn't realize you could do INSERT queries like that. Looks like I'm behind the times, since it was added with version 3.22.10:

http://www.mysql.com/doc/en/INSERT.html

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] Breaking Up a Line using PHP In reply to
In Reply To:
Yep - my mistake. I didn't realize you could do INSERT queries like that. Looks like I'm behind the times, since it was added with version 3.22.10:

LOL.. that is how I got shown to do it. Using VALUES ( ) was new to me when I started with MySQL. I guess different people learn different ways Crazy

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Breaking Up a Line using PHP In reply to
Yeah. I learnt both ways but I find the way I do it easier for me, just personal preference.